编程语言
首页 > 编程语言> > android-以编程方式将视图添加到LinearLayout不会在除一个以外的所有内容上设置文本

android-以编程方式将视图添加到LinearLayout不会在除一个以外的所有内容上设置文本

作者:互联网

我有一个线性布局,想动态添加可变数量的列表项. (它不能是回收站视图,因为此列表已经在回收站视图中,并且被认为是一个很小的列表.)但是由于某些原因,它没有保存我设置的文本(最后一项除外).如果我添加了另一个标题不同的项目,则将显示最后添加的项目,其他项目将显示“默认”.有人知道发生了什么吗?真的很奇怪谢谢!

这是代码:

settings_item.xml

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

    <TextView
        style="?android:listSeparatorTextViewStyle"
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#00000000"
        android:text="Default"
        android:layout_weight="1"/>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end|center_vertical"
        android:layout_weight="0"/>

</LinearLayout>

root_settings_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginEnd="5dp">
</LinearLayout>

SettingsView.java:

public class SettingsView extends LinearLayout {

    //Views
    private View view;

    /**
     * Constructor for android tools to use.
     */
    public SettingsView(Context context) {
        super(context);
    }

    /**
     *
     * @param parent
     */
    public SettingsView(ViewGroup parent) {
        super(parent.getContext());

        //Get the inflated layout
        LinearLayout view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);


        View itemView = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
        ((TextView) itemView.findViewById(R.id.title)).setText("Hi");
        ((CheckBox) itemView.findViewById(R.id.checkbox)).setChecked(true);


        View itemView2 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
        ((TextView) itemView2.findViewById(R.id.title)).setText("Hello");
        ((CheckBox) itemView2.findViewById(R.id.checkbox)).setChecked(false);

        View itemView3 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
        ((TextView) itemView3.findViewById(R.id.title)).setText("Bye");
        ((CheckBox) itemView3.findViewById(R.id.checkbox)).setChecked(true);

        View itemView4 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
        ((TextView) itemView4.findViewById(R.id.title)).setText("Test");
        ((CheckBox) itemView4.findViewById(R.id.checkbox)).setChecked(false);

        addView(view);
    }

Result

解决方法:

感谢@Rod_Algonquin的评论,这使我确信必须为此制作一个小部件.幸运的是,我之前做过小部件,所以我知道该怎么做.我只是想避免这种情况,但是如果我们不使用小部件,我想它在重复ID方面会出现问题.

以下代码使它起作用:

添加了新类SettingItemView.java:

/**
 * A item view with the title and a checkbox.
 */
public class SettingItemView extends LinearLayout {

    private TextView mTitle;
    private CheckBox mCheckBox;

    public SettingItemView(Context context) {
        super(context);
        inflate(context, R.layout.settings_item, this);
        mTitle = (TextView) findViewById(R.id.title);
        mCheckBox = (CheckBox) findViewById(R.id.checkbox);
    }

    public void setTitle(String title) {
        mTitle.setText(title);
    }

    public void setCheckbox(boolean checked) {
        mCheckBox.setChecked(checked);
    }
}

更改了此构造方法:

/**
 *
 * @param parent
 */
public SettingsView(ViewGroup parent) {
        super(parent.getContext());

        //Get the inflated layout.
        LinearLayout view = LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);


        SettingItemView itemView = new SettingItemView(parent.getContext());
        itemView.setTitle("Hi");
        itemView.setCheckbox(true);
        view.addView(itemView);

        SettingItemView itemView2 = new SettingItemView(parent.getContext());
        itemView2.setTitle("Hello");
        itemView2.setCheckbox(true);
        view.addView(itemView2);

        SettingItemView itemView3 = new SettingItemView(parent.getContext());
        itemView3.setTitle("Bye");
        itemView3.setCheckbox(true);
        view.addView(itemView3);

        addView(view);
    }

标签:layout-inflater,android
来源: https://codeday.me/bug/20191111/2022258.html