重用具有少量变化的视图组

Reuse View group with small variety

提问人:Simon Van den Bossche 提问时间:9/3/2023 更新时间:9/3/2023 访问量:21

问:

我是 android 应用程序编程的新手,我很好奇重用包含一些略有差异的视图的布局。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            style="@style/Font"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/lbl_graph_last" />

        <TextView
            android:id="@+id/last"
            style="@style/Font"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="end" />
    </LinearLayout>

我需要多次将此布局与内部的 2 个文本视图一起使用,唯一的区别是:第一个 TextView 中的文本,我还需要以编程方式更改第二个 TextView 的文本(我计划为此使用 id,但我愿意接受更好的建议)。android:text="@string/lbl_graph_last"

人造人

评论

0赞 Pawan Harariya 9/3/2023
您可以按照本教程创建自己的自定义视图。然后,您可以在任何地方轻松使用它。
0赞 Pawan Harariya 9/3/2023
如果您的数据是动态的,并且您事先不知道这些布局中有多少,您也可以使用回收器视图。

答:

2赞 Pawan Harariya 9/3/2023 #1

您可以创建自定义视图,然后在任何位置使用它,就像 xml 中的常规视图一样。有关更多详细信息,请遵循本教程

  1. 为自定义视图创建布局。确保此布局的根标记是 。<merge>
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_first"
            android:layout_width="wrap_content"
            android:textColor="@color/black"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text_second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:layout_weight="1"
            android:gravity="end" />
    </LinearLayout>
</merge>
  1. 定义要可配置的属性,然后在文件中定义这些属性。您可以在文件夹内创建文件。attrs.xmlattrs.xmlvalues
<declare-styleable name="CustomView">
        <attr name="text_first" format="string" />
        <attr name="text_second" format="string" />
</declare-styleable>
  1. 创建自定义视图
public class CustomView extends LinearLayout{
    private TextView textFirst, textSecond;

    public CustomView(@NonNull Context context) {
        super(context);
    }

    public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        if (context == null || attrs == null)
            return;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0);
        String first = a.getString(R.styleable.CustomView_text_first);
        String last = a.getString(R.styleable.CustomView_text_second);
        a.recycle();
        inflate(getContext(), R.layout.layout_custom_view, this);
        textFirst = findViewById(R.id.text_first);
        textSecond = findViewById(R.id.text_second);
        textFirst.setText(first);
        textSecond.setText(last);
    }
    
    // you can expose methods for attributes you want to set programatically
    public void setSecondText(String text) {
        textSecond.setText(text);
    }
}
  1. 使用自定义视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">


    <com.example.CustomView
        android:id="@+id/top_custom_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:text_first="Hi..."/>

</LinearLayout>
  1. 以编程方式更改属性
public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMain2Binding binding = ActivityMain2Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        CustomView customView = binding.topCustomView;
        customView.setSecondText("How are you?");
    }
}

示例输出:

enter image description here