我想在java中设置CustomText怎么做?

时间:2015-06-25 04:24:13

标签: android text

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    LinearLayout ll=new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT));

    TextView txtview=new TextView(this);
    txtview.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
   CustomTextView ct=new CustomTextView(this);


    txtview.setText(ct.setTypeface(tf));
    ll.addView(txtview);

//        here i want to set Custom Text
//        here i want to custom style of text in other class

     txtview.setText("Text");  


    setContentView(ll);
}



}

//    ohter class of text style

public class CustomTextView extends TextView {

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    Log.e("Tag", "AttributeSet with style");
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    Log.e("Tag", "AttributeSet");
}

public CustomTextView(Context context) {
    super(context);
    Log.e("Tag", "context");
}

@Override
public void setTypeface(Typeface tf) {
    Typeface typeface = Typeface.createFromAsset(
            getResources().getAssets(), "COMICATE.TTF");
    super.setTypeface(typeface);
}

}

2 个答案:

答案 0 :(得分:0)

因为CustomTextView类扩展了TextView,所以可以在TextView中使用所有方法声明。 所以,ct.setText("你的文字在这里")应该有效。 并确保将自定义textview添加到您的布局

答案 1 :(得分:0)

从查看代码我理解的是txtview.setText(ct.setTypeface(tf));行,你得到了编译时错误。 所以改变你的代码,如

ct.setTypeface(tf);
ct.setText("Your text");
ll.addView(ct);