以编程方式向LinearLayout添加视图而不是在除一个之外的所有文本上设置文本

时间:2017-01-30 21:26:27

标签: android layout-inflater

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

以下是代码:

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

2 个答案:

答案 0 :(得分:2)

感谢@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);
    }

答案 1 :(得分:1)

你基本上是在运行它而只添加最后一个,这段代码应该可以工作:

public SettingsView(ViewGroup parent){         超级(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);
    addView(view);
    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);
    addView(view);
    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);
    addView(view);
    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);