【问题标题】:Compound control in other package其他包中的复合控制
【发布时间】:2012-04-19 10:26:28
【问题描述】:

我添加了一个简单的复合控件,它从 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>

但是好像不能用。无论我如何使用 CH2,XML 视图总是显示 Unexpected text found in layout file:

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>

【问题讨论】:

  • 第一种方法的错误是什么?只是参数没有通过?
  • 你说得对,这行得通。一开始它只是没有,因为有一个 XML 错误(eclipse 无法识别)。所以这很完美!我只需要清理项目并重新启动 eclipse 即可使所有命名空间更改生效。

标签: android namespaces custom-controls packages


【解决方案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>

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

【讨论】:

  • 这不起作用,XML 视图仍然显示“在布局文件中找到意外的文本:myns:textText...”
  • @ZoolWay 我假设您在布局顶部声明了命名空间和架构?
猜你喜欢
  • 1970-01-01
  • 2011-08-30
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多