2

カスタム ボタン、TextView、および ImageView を作成しました。これらのいずれも、XML のグラフィカル レイアウトで適切に表示されません。カスタム フォントでボタンやテキストを表示する代わりに、呼び出しているカスタム クラスの名前を示す巨大な灰色のボックスが表示されます。これらをプレビューに表示するにはどうすればよいですか?

public class FontTextView extends TextView {

public static Typeface FONT_NAME;

public FontTextView(Context context) {
    super(context);
    if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
    this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
    this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
    this.setTypeface(FONT_NAME);
}

<com.example.gesturetest.FontTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text placeholder" />
4

1 に答える 1

5

カスタムビューでこれを行うことができます:

if(!isInEditMode()){
   // Your custom code that is not letting the Visual Editor draw properly
   // i.e. thread spawning or other things in the constructor
}

参照

customView のコンストラクターでこれを行います

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (!isInEditMode()) {
        createTypeface(context, attrs); //whatever added functionality you are trying to add to Widget, call that inside this condition. 
    }

}
于 2014-12-04T10:49:15.503 に答える