根据这个 CommonsWare 示例,我设法让我的 RelativeLayout 子类与我在具有合并根的 xml 布局中描述的布局合并。我唯一担心的是我无法在 xml 中描述我的 RelativeLayout 参数。
我的 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res/hu.someproject"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:width="36dp" >
<RelativeLayout
android:id="@+id/upper_container"
style="?pretty_style"
android:layout_alignLeft="@+id/image_title"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/image_title"
android:layout_centerHorizontal="true" >
<View
android:id="@+id/upper_indicator"
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_alignParentTop="true"
android:background="@color/mycolor" />
<TextView
android:id="@+id/text_degree"
style="?pretty_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="23°" />
</RelativeLayout>
<hu.MyView
android:id="@+id/image_title"
style="?my_image_title"
android:layout_below="@+id/upper_container"
android:layout_centerHorizontal="true"
my:myColor="#2f8741"/>
我认为问题在于合并发生在合并标签的子项上,而不是合并本身。知道如何在合并中获取我的参数以影响我的 RelativeLayout 吗?
我的 RelativeLayout 子类,没有包声明和导入:
public class MyRelativeLayoutSubclass extends RelativeLayout {
public MyRelativeLayoutSubclass(Context context) {
super(context);
initTile(null);
}
public MyRelativeLayoutSubclass(Context context, AttributeSet attrs) {
super(context, attrs);
initTile(attrs);
}
public MyRelativeLayoutSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initTile(attrs);
}
private void initTile(Object object) {
final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_great_little_layout, this, true);
}
}
我知道我可以将所有内容添加到 LayoutParams,然后将我的 MyRelativeLayoutSubclass 添加到 LayoutParams,但我想避开它,这是很多不必要的代码。
最佳答案
I think the problem is that merge happens to the children of the merge tag, and not the merge itself.
据我所知,你是对的。 <merge>
是占位符,不是 ViewGroup
.
I know I can add everything to a LayoutParams, and then add my MyRelativeLayoutSubclass with that LayoutParams, but I would like escape that, that's a lot of unnecessary code.
创建一个包含 MyRelativeLayoutSubclass
的 XML 布局文件元素,并将你的属性放在那里。然后,膨胀该布局。
关于android - 通过 inflate 从合并布局到 RelativeLayout 的 XML 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8941038/