简体   繁体   English

如何在Android应用程序开发中使通用布局包含在其他布局中,但如何动态更改通用布局内部的属性?

[英]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. 我对android非常陌生,所以请原谅我的无知。

My main layout is a vertical LinearLayout, I want to include other repeating layouts in this. 我的主要布局是垂直的LinearLayout,我想在其中包括其他重复的布局。 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. 例如,包含的通用布局将是水平的LinearLayout,并将具有TextView和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? 如何动态更改每个包含的TextView和EditText中的文本? 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? 如何动态更改每个包含的TextView和EditText中的文本?

You would do this by giving each unique id s. 您可以通过指定每个唯一的id来做到这一点。 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 . 然后在需要访问它们的Views时给它们充气。 Suppose include1 is a LinearLayout 假设include1LinearLayout

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

then get the TextView from that. 然后从中获取TextView Suppose the TextView in the included layout has an id of textView1 假设包含layoutTextViewidtextView1

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

Now you can do what you want with tv1 现在您可以用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 您可以在要添加这些布局的任何父布局上调用addView()

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: 您可以创建自己的自定义MyLinearLayout extends LinearLayout并使用自己的ID绑定视图:

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: 如果您需要重复添加一些相似的视图(或从一种布局中完全相同),则可以使用基本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. 但是请记住,您的ID站台不能与同一布局中的其他ID交叉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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