Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何设置多个文本视图';s码_Android - Fatal编程技术网

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
中定义。现在我想根据设置设置大小,如小型、中型和大型。这是我登录时收到的
整数
值,但我知道

无法从my
activity
以编程方式设置my
dimen.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);
}
这是一个漫长的过程。还有什么其他的方法是好的吗

  • 编写一个接受holder和size的helper函数
  • 在helper函数中,然后搜索循环中的每个子对象
  • 如果子视图是TextView,则将其强制转换为TextView,并调用setTextSize函数,该函数的大小在步骤1中传递给帮助器

  • 我建议您使用自定义
    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上执行的循环中