如何提取目前Android布局细分到自己的自定义控件? [英] How to extract current Android layout segments into their own custom control?

查看:206
本文介绍了如何提取目前Android布局细分到自己的自定义控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,我建立了一个完整的布局;但是,我不是很高兴与导致长XML文件。我有XML大纲及以下设计视图的短路版本。我想知道我怎么可以抽象出每组相似的组件到自己的自定义控件。

例如,下面的图片中,我强调这样一个控制,我想抽象出来。相反,它是一个的LinearLayout 与2 的TextView 的里面加上自己的属性和属性集。我想通过&LT引用它;包name.individual_song_item
机器人:布局...> ...< />
。所有我需要做的是通过在顶级组件的属性,第二个沿设定的第一个的TextView 的文字。

如何才能做到这一点?我已经布局完成,完成,但我不喜欢,没有什么是抽象化了。

所以,我要寻找的预期结果(如果你看一下图片的右侧。也就只有一个的LinearLayout 以下图像,剩下的将是<包name.individual_song_item>

我试图刚刚创建的组件刚子集布置一新的XML,但我无法使合并回来时,它的工作。


老办法

 <&的LinearLayout GT;    < ImageView的/>    <&的LinearLayout GT;        <&的LinearLayout GT;            <的TextView />
            <的TextView />        < / LinearLayout中>        <&的LinearLayout GT;            <的TextView />
            <的TextView />        < / LinearLayout中>        <&的LinearLayout GT;            <的TextView />
            <的TextView />        < / LinearLayout中>        ....    < / LinearLayout中>< / LinearLayout中>

可能提出WAY

 <&的LinearLayout GT;    < ImageView的/>    <&的LinearLayout GT;        < com.example.individual_song_item />
        < com.example.individual_song_item />
        < com.example.individual_song_item />        ....        < com.example.individual_song_item<! - 例如(!?!?可能) - >
            ....
            应用:标签=集团
            应用:价值=组名/>    < / LinearLayout中>< / LinearLayout中>


解决方案

创建一个自定义布局如:

 公共类IndividualSongItem扩展的LinearLayout {    私人字符串mSong;
    私人字符串mSongName;    公共IndividualSongItem(上下文的背景下){
        超级(上下文);
        // TODO自动生成构造函数存根
    }    公共IndividualSongItem(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        TypedArray A = context.obtainStyledAttributes(ATTRS,R.styleable.IndividualSongItem);        尝试{
            //读取您的自定义布局的属性,
            //例如歌文本SONGNAME属性
            CharSequence中= a.getString(R.styleable.IndividualSongItem_song);
            如果(S!= NULL){
                setSong(s.toString());
            }            S = a.getString(R.styleable.IndividualSongItem_songName);
            如果(S!= NULL){
                setSongName(s.toString());
            }
        } {最后
            a.recycle();
        }    }
....等等

您还需要为你的新布局类创建一个XML属性。
有关如何做你看一下在ApiDemos的LABELVIEW示例后是什么完整的例子。

它也很好的解释了 <一个href=\"http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml\">here.

OK, I have a complete layout built; however, I am not really pleased with the long xml file that has resulted. I have a shorted version of the xml outline and designer view below. And I was wondering how I can abstract out each group of similar components into their own custom control.

For example, in the picture below, I have highlighted one such control that I would like to abstract out. Instead of it being a LinearLayout with 2 TextView's inside with their own properties and attributes set. I would like to reference it via <package-name.individual_song_item android:layout...> ... </>. All I would have to do is set the first TextView's text along with the second one via attributes in the top-level component.

How can this be done? I have the layout done and complete, but I don't like that nothing is abstracted away.

So the expected results that I am looking for are (if you look at the right-side of the image. there would only be one LinearLayout below the image, and the rest would be <package-name.individual_song_item>)

I have tried to just create a new layout xml with just the subsets of components, but I was not able to make it work when combining it back.


OLD WAY

<LinearLayout >

    <ImageView />

    <LinearLayout >

        <LinearLayout >

            <TextView />
            <TextView />

        </LinearLayout>

        <LinearLayout >

            <TextView />
            <TextView />

        </LinearLayout>

        <LinearLayout >

            <TextView />
            <TextView />

        </LinearLayout>

        ....

    </LinearLayout>

</LinearLayout>

POSSIBLE PROPOSED WAY

<LinearLayout >

    <ImageView />

    <LinearLayout >

        <com.example.individual_song_item />
        <com.example.individual_song_item />
        <com.example.individual_song_item />

        ....

        <com.example.individual_song_item  <!-- example (possible!?!?) -->
            ....
            app:label="Group"
            app:value="Group Name" />

    </LinearLayout>

</LinearLayout>

解决方案

Create a custom layout eg.

public class IndividualSongItem extends LinearLayout {

    private String mSong;
    private String mSongName;

    public IndividualSongItem(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public IndividualSongItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IndividualSongItem);

        try {
            // Read in your custom layout's attributes, 
            // for example song and songName text attributes
            CharSequence s = a.getString(R.styleable.IndividualSongItem_song);
            if (s != null) {
                setSong(s.toString());
            }

            s = a.getString(R.styleable.IndividualSongItem_songName);
            if (s != null) {
                setSongName(s.toString());
            }
        } finally {
            a.recycle();
        }

    }
....etc

You will also need to create an attributes XML for your new layout class. For a full example of how to do what you're after look at the LabelView example in the ApiDemos.

It's also very well explained here.

这篇关于如何提取目前Android布局细分到自己的自定义控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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