Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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 避免重复的xml代码_Android_Android Layout - Fatal编程技术网

Android 避免重复的xml代码

Android 避免重复的xml代码,android,android-layout,Android,Android Layout,我想实现类似的目标 我不知道如何在不为水平线下方的信息创建12个单独的文本视图的情况下创建绿色布局 我确实有一个activity\u home.xml文件,其中包含 <LinearLayout android:id="@+id/home_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"

我想实现类似的目标

我不知道如何在不为水平线下方的信息创建12个单独的文本视图的情况下创建绿色布局

我确实有一个
activity\u home.xml
文件,其中包含

<LinearLayout
    android:id="@+id/home_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="11dp"
        android:layout_weight="0.4"
        android:background="#118675"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="15dp">

            <TextView
                android:id="@+id/main_strength_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Strength score: "
                android:textColor="@color/main_text_green"
                android:textSize="22sp" />

            <TextView
                android:id="@+id/main_strength_level"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/main_strength_number"
                android:text="Placeholder"
                android:textColor="@color/secondary_text_green" />

            <View
                android:id="@+id/home_horizontal_line"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/main_strength_level"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="15dp"
                android:background="@color/secondary_text_green" />


        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="11dp"
        android:layout_weight="0.4"
        android:background="#ffffff"
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>
看起来像这样

然而,我不确定如何组合这两个文件来实现与第一张图片中的应用程序类似的功能,或者这是否是一个正确的开始方法


我也不确定,如果我真的将4个
home\u exercise.xml
布局合并到
activity\u home.xml
,我将如何区分所有文本视图彼此之间的差异如果行数不是恒定的,请使用带有自定义适配器的ListView或RecyclerView

如果行数不变,则可以在xml中使用
include
标记。include标记允许您指定ID

XML示例:

<include
    android:id="@+id/home_exercise_one"
    layout="@layout/home_exercise" />


然后是一个简单的
findviewbyd(R.id.home\u exercise\u one)
调用Java或Kotlin。要引用包含的xml的子项,必须像这样链接findViewById:
findViewById(R.id.home\u exercise\u one)。findViewById(R.id.home\u exercise\u name)

使用RecyclerView或ListView。我也在考虑使用
RecyclerView
,但我不认为所有的编码都值得,假设它只有4行(一个常量)。正是我想要的。谢谢:)
<include
    android:id="@+id/home_exercise_one"
    layout="@layout/home_exercise" />