I have the following Layout named
sample_layout.xml
and I want to use it multiple times but each time with a few changes.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginBottom="20dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="110dp" android:background="@drawable/full_panel_blue" android:padding="10dp" android:layout_marginBottom="20dp"> <ImageView android:layout_width="120dp" android:layout_height="120dp" android:src="@mipmap/first_icon" /> <LinearLayout android:layout_width="350dp" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:gravity="end" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/my_white" android:text="4000000" android:textSize="50sp" android:gravity="end"/> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="end" android:textColor="@color/my_white" android:textSize="20sp" android:text="Sample text" /> </LinearLayout> </RelativeLayout> </LinearLayout>
Lower is the layout where it will be included multiple times
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include layout="@layout/sample_layout"/> <include layout="@layout/sample_layout"/> <include layout="@layout/sample_layout"/> </LinearLayout>
So, I want to include it first time just as it is, but the second time instead of the
@drawable/full_blue_panel
, I want to use another drawable (@drawable/red_w_transparency
). And also for theImageView
I want to use anothersrc
for the icon second time, and another one the third time. Maybe different text in theTextView
each time and so on for any of the previously created attributes. I already tried something with DataBinding but I don't think it is exactly suitable for this. Is there any other way to this?
最佳答案
一般来说,我通过创建自定义 View
来解决这类问题。子类。此处介绍了基础知识:https://developer.android.com/guide/topics/ui/custom-components.html#compound
简短的版本是您定义一些影响您要更改的部分的自定义属性,然后创建一个 View
读取这些属性以修改其 View 层次结构的子类实现。
在您发布的示例中,您有以下相关要点:
- 根是一个
LinearLayout
- 您想更改
RelativeLayout
背景 - 您想更改
ImageView
来源 - 您想更改
TextView
正文
您可以在 attrs.xml
中创建这些属性:
<declare-styleable name="MyCompoundComponent">
<attr name="background"/>
<attr name="imageSrc"/>
<attr name="primaryText"/>
</declare-styleable>
接下来,您将创建您的 View 类。由于您的原始布局的根是 LinearLayout
,我们将从中得出:
public class MyCompoundComponent extends LinearLayout {
public MyCompoundComponent(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCompoundComponent);
// read your attributes
a.recycle();
inflate(context, R.layout.my_compound_component, this);
// find your views, use your attributes to modify them
}
}
当然,您需要布局。您可以只使用上面发布的那个,除非您将根元素更改为 <merge>
现在你在 LinearLayout
里面给它充气了.
有了所有这些,您就可以在其他布局中使用此 View ,就像使用任何其他 View 一样:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.stackoverflow.MyCompoundComponent
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:background="@color/blue"
app:imageSrc="@drawable/image1"
app:primaryText="@string/text1"/>
<com.example.stackoverflow.MyCompoundComponent
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:background="@color/red"
app:imageSrc="@drawable/image2"
app:primaryText="@string/text2"/>
</LinearLayout>
关于android - 除了DataBinding之外,还有其他方法可以在Android Studio中多次使用具有不同属性的相同布局吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46982922/