![](/img/trans.png)
[英]How can I include a custom view in an existing XML layout on Android?
[英](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>
这仅用于表达我的问题,而不是我的生产环境中使用的真实布局。 每个“东西”都是指特定的布局,它们是如此不同,以至于我不能使用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>
提前致谢!
您可以使用 ,并根据需要在其布局中设置每个视图,但为此,您需要为每个视图创建一个 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.