How to extract current Android layout segments into their own custom control?

旧巷老猫 提交于 2019-12-11 14:54:58

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/18927997/how-to-extract-current-android-layout-segments-into-their-own-custom-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!