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

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

问题描述

好的,我已经构建了一个完整的布局;但是,我对生成的长 xml 文件并不满意.我有下面的 xml 大纲和设计器视图的简短版本.我想知道如何将每组相似的组件抽象到他们自己的自定义控件中.

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.

例如,在下图中,我突出显示了一个我想抽象出来的控件.它不是一个 LinearLayout,里面有 2 个 TextView 和它们自己的属性和属性集.我想通过 <package-name.individual_song_item 引用它安卓:布局...>... </>.我所要做的就是通过顶级组件中的属性设置第一个 TextView 的文本和第二个文本.

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.

所以我正在寻找的预期结果是(如果您查看图像的右侧.图像下方将只有一个 LinearLayout,其余的将)

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>)

我曾尝试仅使用组件的子集创建一个新的布局 xml,但在将其组合回来时我无法使其工作.

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.

老路

<LinearLayout >

    <ImageView />

    <LinearLayout >

        <LinearLayout >

            <TextView />
            <TextView />

        </LinearLayout>

        <LinearLayout >

            <TextView />
            <TextView />

        </LinearLayout>

        <LinearLayout >

            <TextView />
            <TextView />

        </LinearLayout>

        ....

    </LinearLayout>

</LinearLayout>

可能的建议方法

<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

您还需要为新布局类创建属性 XML.有关如何执行操作的完整示例,请查看 ApiDemos 中的 LabelView 示例.

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天全站免登陆