Избегайте повторяющегося кода XML - PullRequest
0 голосов
/ 07 июня 2018

Я хочу добиться чего-то похожего на

enter image description here

Я не знаю, как создать зеленый макет без создания 12 отдельных текстовых представлений дляинформация под горизонтальной линией.

У меня есть файл activity_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>

И выглядит как

enter image description here

Игнорировать кнопку справа внизу

У меня также есть home_exercise.xml, который содержит 3 просмотра текста, для названия упражнения, уровня силы и номера силы, например на первой фотографии, которая может быть «Присед»«промежуточный» и «72,2»

код выглядит следующим образом

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/home_exercise_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:text="name placeholder"
        android:textColor="@color/main_text_green" />

    <TextView
        android:id="@+id/home_exercise_level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:text="72.2"
        android:textAlignment="textEnd"
        android:textColor="@color/main_text_green"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/home_exercise_strength"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@id/home_exercise_name"
        android:text="Intermediate"
        android:textColor="@color/secondary_text_green" />


</RelativeLayout>

и выглядит следующим образом

enter image description here

Однако я не уверен, как объединить эти 2 файла, чтобы получить нечто похожее на приложение на первом рисунке, или если это даже правильный подход для начала.

Я также не уверен,если я объединю 4 из home_exercise.xml макетов в activity_home.xml, как я смогу дифференцировать все TextViews от каждогодругие

1 Ответ

0 голосов
/ 07 июня 2018

Если число строк не является постоянным, используйте ListView или RecyclerView с настраиваемым адаптером.

Если количество строк является постоянным, вы можете использовать тег include в своем xml.Тег include позволяет указать идентификаторы.

XML Пример:

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

Тогда это простой вызов findViewById(R.id.home_exercise_one) в вашем Java или Kotlin.Чтобы сослаться на дочерние элементы включенного xml, вам нужно будет связать ваши findViewByIds следующим образом: findViewById(R.id.home_exercise_one).findViewById(R.id.home_exercise_name)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...