其他包装中的复合控制

时间:2012-04-05 16:50:07

标签: android namespaces custom-controls packages

我添加了一个简单的复合控件,它从XML布局加载其布局并实现自己的XML属性。

但这仅适用于此控件位于主程序包(com.myapp)中。当我试图将它移动到像com.myapp.controls这样的子包中时,事情开始失败。

这是用于定义控件自定义属性的attrs.xml:

<resources>

    <declare-styleable name="CH2">
        <attr name="textText" format="string" />
    </declare-styleable>

    <declare-styleable name="CH3">
        <attr name="textFex" format="string" />
    </declare-styleable>

</resources>

这是控件CH2.java,它在子包中并且工作:

package com.myapp.controls;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CH2 extends LinearLayout {

    private TextView textView;

    public CH2(Context context) throws Exception {
        super(context);
        init(context, null);
    }

    public CH2(Context context, AttributeSet attrs) throws Exception {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) throws Exception {

        this.setOrientation(LinearLayout.VERTICAL);

        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(com.myapp.R.layout.category_header, this, true);

        View v = findViewById(com.myapp.R.id.chText);
        this.textView = (TextView)v;

        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, com.myapp.R.styleable.CH2);

            String text = a.getString(com.myapp.R.styleable.CH2_textText);
            this.textView.setText(text != null ? text : "<NULL!>");

            a.recycle();        
        }
    }
}

这是CH3.java的作用:

package com.myapp;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CH3 extends LinearLayout {

    private TextView textView;

    public CH3(Context context) throws Exception {
        super(context);
        init(context, null);
    }

    public CH3(Context context, AttributeSet attrs) throws Exception {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) throws Exception {

        this.setOrientation(LinearLayout.VERTICAL);

        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(com.myapp.R.layout.category_header, this, true);

        View v = findViewById(com.myapp.R.id.chText);
        this.textView = (TextView)v;

        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, com.myapp.R.styleable.CH3);

            String text = a.getString(com.myapp.R.styleable.CH3_textFex);
            this.textView.setText(text != null ? text : "<NULL!>");

            a.recycle();        
        }
    }
}

两个来源都使用相同的category_header.xml。布局加载没有问题。我可以这样使用:

<com.myapp.controls.CH2
    android:id="@+id/cH1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    myns:textText="Test 1" >
</com.myapp.controls.CH2>

<com.myapp.CH3
    android:id="@+id/cH2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    myns:textFex="Test 2"
    >
</com.myapp.CH3>

如何使CH2工作?或者是否不可能使用另一个包裹?

Sidequest:我可以让两个控件都使用相同的XML属性textText,还是总是必须使用不同的属性?或者声明式样名称可以与组件名称不同吗?


使用新的declare-styleable的另一种方法:

<declare-styleable name="controls.CH2">
    <attr name="textText" format="string" />
</declare-styleable>

但它似乎没有用。 XML视图总是说在布局文件中找到意外的文本:,无论我如何使用CH2:

xmlns:myns="http://schemas.android.com/apk/res/com.myapp"
xmlns:mynsc="http://schemas.android.com/apk/res/com.myapp.controls"
...
<com.myapp.controls.CH2
    android:id="@+id/cH1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    mynsc:textText="TestTestTest" 
    >        
</com.myapp.controls.CH2>

xmlns:myns="http://schemas.android.com/apk/res/com.myapp"
...
<com.myapp.controls.CH2
    android:id="@+id/cH1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    myns.controls:textText="TestTestTest" 
    >        
</com.myapp.controls.CH2>

1 个答案:

答案 0 :(得分:1)

我刚回到我自己的属性文件中,并且在我的布局所在的非常深的子包中没有任何引用...作为旁注,你可以让多个元素使用相同的属性,如果你在attrs档案中这样做:

<resources>
    <attr name="textText" format="string"/>

    <declare-styleable name="CH2">
        <attr name="textText" />
    </declare-styleable>

    <declare-styleable name="CH3">
        <attr name="textText" />
    </declare-styleable>

</resources>

我对android源代码的研究显示了一些示例,它们完全按照您所描述的内容进行操作,其中有一个自定义属性文件指向不同包中的视图类。我看到的唯一明显的区别是视图在布局中声明如下:

<view
  class="com.myapp.controls.CH2"
  android:id="@+id/cH1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
  myns:textText="Test 1" >
</view>

<view
  class="com.myapp.CH3"
  android:id="@+id/cH2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  myns:textFex="Test 2"
>
</view>

老实说,我无法告诉你为什么会有所作为......但每次他们都需要使用自定义属性

相关问题