Android 如何设置多个文本视图';s码
我在Android 如何设置多个文本视图';s码,android,Android,我在ViewHolder 比如: holder.ID1 = (TextView) vi.findViewById(R.id.lbID1); holder.ID2 = (TextView) vi.findViewById(R.id.lbID2); holder.ID3 = (TextView) vi.findViewById(R.id.lbID3); holder.ID4 = (TextView) vi.findViewById(R.id.lbID4); and so on.... 属性和大小
ViewHolder
比如:
holder.ID1 = (TextView) vi.findViewById(R.id.lbID1);
holder.ID2 = (TextView) vi.findViewById(R.id.lbID2);
holder.ID3 = (TextView) vi.findViewById(R.id.lbID3);
holder.ID4 = (TextView) vi.findViewById(R.id.lbID4); and so on....
属性和大小在style.xml
和dimens.xml
中定义。现在我想根据设置设置大小,如小型、中型和大型。这是我登录时收到的整数
值,但我知道
无法从myactivity
以编程方式设置mydimen.xml
文件中的dimen变量值
因此,一种方法是如下设置大小:
setSize(holder.ID1, Size);
setSize(holder.ID2, Size);
setSize(holder.ID3, Size);
setSize(holder.ID4, Size); and so on....
public void setSize(TextView textView, int size) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
}
这是一个漫长的过程。还有什么其他的方法是好的吗
我建议您使用自定义
TextView
CustomTextView.java
public class CustomTextView extends android.support.v7.widget.AppCompatTextView {
String Size;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView,0,0);
try{
Size = ta.getString(R.styleable.CustomTextView_ctv_size);
if (Size.equals("small")){
this.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
}else{
this.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
}
}finally {
ta.recycle();
}
}
public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
将attr.xml文件添加到values目录中,并在其中添加以下代码:
只需使用dimens.xml
并用xml设置文本大小。或者你可以为每个文本视图使用样式,比如smallStyle、largeStyle等等。那么我如何在style.xml或dimens.xml中使用if条件,然后使用TypedValue.COMPLEX\u UNIT\u SP
。setTextSize只是一个代码,您需要在每个视图上调用它,所以我认为您可以单独调用它。不需要实用类。您可以使用其他选项,如中型和大型。。。对于所有的文本视图,您可以设置其他文本视图大小值…这可能会起作用,但不适合,因为OP需要一个简短的解决方案,而不必处理每个文本视图。他不必处理每个文本视图。只写一次。代码在每个textview上执行的循环中