繁体   English   中英

(Android Layout) 如何自定义一个可以简单包含其他View的View?

[英](Android Layout) How to custom a View that can simply contains other View?

例如,我有一个活动,它的布局 xml 文件是这样的:

<?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>

这仅用于表达我的问题,而不是我的生产环境中使用的真实布局。 每个“东西”都是指特定的布局,它们是如此不同,以至于我不能使用ListViewRecyclerView

如您所见,每个“东西”都有类似的CardViewScrollViewLinearLayout ,所以我想知道是否可以将它们作为自定义视图来简化我的布局,如下代码所示?

<?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>

提前致谢!

您可以使用 ,并根据需要在其布局中设置每个视图,但为此,您需要为每个视图创建一个 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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM