创建复合控件使用自定义XML属性 [英] Creating Compound Controls With Custom XML Attributes

查看:189
本文介绍了创建复合控件使用自定义XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想一个TextView和一个EditText组合成一个复合控件,它使用自定义XML元素来传递默认值为每个单独的元素。我一直在寻找教程/文档浏览:
<一href="http://m.androidside.com/docs/docs1.5/guide/topics/ui/custom-components.html#compound">Building复合控件
<一href="http://blog.pocketjourney.com/2008/05/02/android-tutorial-42-passing-custom-variables-via-xml-resource-files/">Passing自定义属性

我有这么远。<​​/ P>

Attrs.xml:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
    &LT;申报,设置样式名称=FreeText的&GT;
        &LT; attr指示NAME =标签格式=字符串/&GT;
        &LT; attr指示NAME =默认格式=字符串/&GT;
    &LT; /申报,设置样式&GT;
&LT; /资源&GT;
 

我的主要布局:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:的myapp =htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    &GT;
    &LT; com.example.misc.FreeText
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        的myapp:标签=标签
        的myapp:默认=默认
    /&GT;
&LT; / LinearLayout中&GT;
 

我的复合控制,FreeText的:

 公共类FreeText的扩展的LinearLayout {

    TextView的标签;
    的EditText值;

    公共FreeText的(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);

        this.setOrientation(水平);

        的LayoutParams LP =新的LayoutParams(0,LayoutParams.WRAP_CONTENT);
        lp.weight = 1;

        标签=新的TextView(上下文);
        addView(标签,LP);

        值=新的EditText(上下文);
        addView(值,LP);

        TypedArray A = context.obtainStyledAttributes(ATTRS,R.styleable.FreeText);
        CharSequence中= a.getString(R.styleable.FreeText_label);
        如果(S!= NULL){
            label.setText(多个);
        }

        a.recycle();
    }
}
 

当我运行程序我看到了意见确定,但我的CharSequence,s的值,始终为空。谁能告诉我,我要去哪里错了?

解决方案

我恨它,你寻求帮助之后,当你注意到这个问题。

但问题是,我的空间我的自定义XML元素本来应该像这样:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:的myapp =htt​​p://schemas.android.com/apk/res-auto
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    &GT;
    &LT; com.example.misc.FreeText
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        的myapp:标签=标签
        的myapp:默认=默认
    /&GT;
&LT; / LinearLayout中&GT;
 

I'm been trying to combine a TextView and an EditText into one compound control which uses custom xml elements to pass in default values for each individual element. I've been looking at the tutorials/docs here:
Building Compound Controls
Passing Custom Attributes

What I have so far.

Attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FreeText">
        <attr name="label" format="string" />
        <attr name="default" format="string" />
    </declare-styleable>
</resources>

My Main Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <com.example.misc.FreeText  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        myapp:label="label"
        myapp:default="default"
    />
</LinearLayout>

My Compound Control, FreeText:

public class FreeText extends LinearLayout {

    TextView label;
    EditText value;

    public FreeText(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setOrientation(HORIZONTAL);

        LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
        lp.weight = 1;

        label = new TextView(context);
        addView(label, lp);

        value = new EditText(context);
        addView(value, lp);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FreeText);
        CharSequence s = a.getString(R.styleable.FreeText_label);
        if (s != null) { 
            label.setText(s);
        }

        a.recycle();
    }
}

When I run the program I see the views OK but the value of my CharSequence, s, is always null. Can someone tell me where I'm going wrong?

解决方案

I hate it when you notice the problem right after you ask for help.

The problem was that my namespace for my custom XML elements should have been like so:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <com.example.misc.FreeText  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        myapp:label="label"
        myapp:default="default"
    />
</LinearLayout>

这篇关于创建复合控件使用自定义XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆