(Макет Android) Как настроить представление, которое может просто содержать другое представление?

#android

#Android

Вопрос:

Например, у меня есть действие, и его XML-файлу layout нравится это:

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

    <androidx.cardview.widget.CardView
        android:id="@ id/card1"
        android:layout_width="match_parent"
        android:layout_height="64dp">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <!-- something1 -->

            </LinearLayout>
        </ScrollView>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@ id/card2"
        android:layout_width="match_parent"
        android:layout_height="64dp">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <!-- something2 -->

            </LinearLayout>
        </ScrollView>
    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@ id/card3"
        android:layout_width="match_parent"
        android:layout_height="64dp">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <!-- something3 -->

            </LinearLayout>
        </ScrollView>
    </androidx.cardview.widget.CardView>

</LinearLayout>
  

Это используется только для выражения моей проблемы, а не для реального макета, используемого в моей производственной среде. Каждое «что-то» относится к определенному макету, и они настолько разные, что я не могу использовать ListView или RecyclerView .

Как вы можете видеть, каждое «что-то» имеет похожие CardView , ScrollView и LinearLayout , поэтому мне интересно, возможно ли создать их в виде пользовательского представления, чтобы я мог упростить свой макет в виде следующих кодов?

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

    <com.example.myapplication.myCustomContainer
        android:id="@ id/something1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- something1 -->

    </com.example.myapplication.myCustomContainer>

    <com.example.myapplication.myCustomContainer
        android:id="@ id/something2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- something2 -->

    </com.example.myapplication.myCustomContainer>

    <com.example.myapplication.myCustomContainer
        android:id="@ id/something3"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- something3 -->

    </com.example.myapplication.myCustomContainer>
</LinearLayout>
  

Заранее спасибо!

Ответ №1:

Вы можете использовать и устанавливать каждое из ваших представлений в его макете по мере необходимости, но для этого вам нужно будет создать элемент XML для каждого из них.

Пример:

             <include
                android:id="@ id/ic_something1"
                layout="@layout/something1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <include
                android:id="@ id/ic_something2"
                layout="@layout/something2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

           <include
                android:id="@ id/ic_something3"
                layout="@layout/something3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
  

Комментарии:

1. Спасибо, но таким образом мне все равно приходится копировать похожие CardView , ScrollView и LinearLayout в каждый XML-файл. Это вызовет трудности с обслуживанием и не очень элегантно, особенно когда его нужно повторять более 10 раз. Я ищу какое-то решение, которому нравится пользовательский вид.