简体   繁体   中英

How do I make generic layouts to include in other layouts but change the properties inside the generic layouts dynamically in android app development?

I am very new to android so please forgive my ignorance.

My main layout is a vertical LinearLayout, I want to include other repeating layouts in this. However I want the info in the objects of the included layouts to be changed. For example, the included generic layout will be a horizontal LinearLayout and will have a TextView and an EditText. In my main layout I want to include two of these generic layouts. How would I dynamically change the text in the TextView and the EditText for each include? Also, how would I dynamically change the number of these included layouts?

My main layout would look something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <include android:id="@+id/generic" />
    <include android:id="@+id/generic" />
    <include android:id="@+id/generic" />
    .
    .
    .
</LinearLayout>

My generic layout would something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/generic"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView .../>
    <EditView .../>
</LinearLayout>

Hopefully I was clear enough. Thanks

How would I dynamically change the text in the TextView and the EditText for each include?

You would do this by giving each unique id s. So

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <include android:id="@+id/include1" />
    <include android:id="@+id/include2" />

Then inflate them when you need to access their Views . Suppose include1 is a LinearLayout

LinearLayout ll1 = (LinearLayout) findViewById(R.id.include1);  

then get the TextView from that. Suppose the TextView in the included layout has an id of textView1

TextView tv1 = (TextView) ll1.findViewById(R.id.textView1); 

Now you can do what you want with tv1

Also, how would I dynamically change the number of these included layouts?

You can call addView() on whatever parent layout you want to add these layouts to

If you need to do something dynamically with layouts, then do it in code:

{// add player name
            playerTxt = new RoboTextView(context);
            LayoutParams playerParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            if (useSingleLine) {
                playerParams.addRule(CENTER_VERTICAL);
                playerParams.addRule(ALIGN_PARENT_LEFT);
            } else {
                playerParams.addRule(RIGHT_OF, AVATAR_ID);
                playerParams.addRule(ALIGN_TOP, AVATAR_ID);
            }

            playerTxt.setTextSize(playerTextSize);
            playerTxt.setTextColor(playerTextColor);
            playerTxt.setId(PLAYER_ID);
            playerTxt.setPadding((int) (4 * density), 0, 0, 0);
            playerTxt.setMarqueeRepeatLimit(2);
            playerTxt.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            playerTxt.setFont(FontsHelper.BOLD_FONT);

            addView(playerTxt, playerParams);
        }

You may create your own custom MyLinearLayout extends LinearLayout and use your own ids to bind views:

public static final int AVATAR_ID = 0x00004400;
public static final int PLAYER_ID = 0x00004401;
public static final int RATING_ID = 0x00004402;
public static final int FLAG_ID = 0x00004403;
public static final int PREMIUM_ID = 0x00004404;
public static final int CAPTURED_ID = 0x00004405;
public static final int TIME_LEFT_ID = 0x00004406;

If you need to repeatedly add few similar views(or exact same from one layout) you can use base ID:

public static final int BUTTON_PREFIX = 0x00002000;

and add other widgets(views) by incrementing counter of something like that

int getButtonId(ButtonIds buttonId) {
    return BUTTON_PREFIX + buttonId.ordinal();
}

But keep in mind that your id shold not intercross with other's ids in the same layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM