Android 自定义TextView填充,TextSize属性不起作用
因此,我尝试创建一个自定义textView,它扩展了原生textView。然而,当我在片段中膨胀它时,尽管像background这样的属性起作用,但是padding和textSize这样的属性不起作用。即使我以编程方式将它们添加到构造函数中,它们也无法工作。我一定错过了一些琐碎的事情。请帮忙 自定义视图Android 自定义TextView填充,TextSize属性不起作用,android,textview,Android,Textview,因此,我尝试创建一个自定义textView,它扩展了原生textView。然而,当我在片段中膨胀它时,尽管像background这样的属性起作用,但是padding和textSize这样的属性不起作用。即使我以编程方式将它们添加到构造函数中,它们也无法工作。我一定错过了一些琐碎的事情。请帮忙 自定义视图 public class TextBox extends TextView{ private OneTimeTask oneTimeTask; private Integer
public class TextBox extends TextView{
private OneTimeTask oneTimeTask;
private Integer oneTimeTaskId;
public TextBox (Context context) {
super(context);
appearance();
}
public TextBox (Context context, AttributeSet attrs) {
super(context, attrs);
appearance();
}
public TextBox (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
appearance();
}
public void appearance(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
this.setTextSize(getResources().getDimension(R.dimen.material_micro_text_size));
this.setPadding((int) getResources().getDimension(R.dimen.material_padding),(int) getResources().getDimension(R.dimen.material_small_padding),(int) getResources().getDimension(R.dimen.material_padding),(int) getResources().getDimension(R.dimen.material_small_padding));
this.setGravity(Gravity.CENTER_VERTICAL);
this.setTextColor(getResources().getColor(R.color.white));
this.setText("Not Started");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
Xml
<com.me.p.k.TextBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Test"
android:id="@+id/state"
android:textSize="10sp"
android:gravity="bottom"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:textColor="@color/white"
android:background="@color/pink"
android:paddingTop="16dp"
android:paddingBottom="16dp" />
编辑
添加了Karakuri建议的一部分。添加三参数构造函数
public TextBox (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
appearance();
}
另外,我不会同时在代码和XML中设置属性,因为这很容易混淆。选择其中一个——到目前为止,我看到的所有内容都可以通过定义样式资源并将其应用于视图来设置