Skip to content Skip to sidebar Skip to footer

How To Extract Current Android Layout Segments Into Their Own Custom Control?

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.

Solution 1:

Create a custom layout eg.

publicclassIndividualSongItemextendsLinearLayout {

    private String mSong;
    private String mSongName;

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

    publicIndividualSongItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArraya= context.obtainStyledAttributes(attrs, R.styleable.IndividualSongItem);

        try {
            // Read in your custom layout's attributes, // for example song and songName text attributesCharSequences= 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.

Post a Comment for "How To Extract Current Android Layout Segments Into Their Own Custom Control?"