本文介绍了通过合并从合并布局到RelativeLayout的XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个CommonsWare示例,我设法使我的RelativeLayout子类与具有合并根的xml布局中描述的布局进行合并.我唯一关心的是,我无法在xml中描述我的RelativeLayout参数.

According to this CommonsWare example I managed to get my RelativeLayout subclass to be merged with my layout described in a xml layout with merge root. My only concern is that I cannot describe my RelativeLayout parameters in xml.

我的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吗?

I think the problem is that merge happens to the children of the merge tag, and not the merge itself. Any idea how can I get my parameters in the merge to affect my RelativeLayout?

我的RelativeLayout子类,没有包声明和导入:

My RelativeLayout subclass, without package declaration and imports:

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中,然后使用该LayoutParams添加MyRelativeLayoutSubclass,但是我想逃避一下,这是很多不必要的代码.

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.

推荐答案

AFAIK,您是对的. <merge>是占位符,而不是ViewGroup.

AFAIK, you are correct. <merge> is a placeholder, not a ViewGroup.

创建一个包含MyRelativeLayoutSubclass元素的XML布局文件,并将属性放在此处.然后,放大该布局.

Create an XML layout file containing a MyRelativeLayoutSubclass element, and put your attributes there. Then, inflate that layout.

这篇关于通过合并从合并布局到RelativeLayout的XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:37