
我有一个线性布局,想动态添加可变数量的列表项. (它不能是回收站视图,因为此列表已经在回收站视图中,并且被认为是一个很小的列表.)但是由于某些原因,它没有保存我设置的文本(最后一项除外).如果我添加了另一个标题不同的项目,则将显示最后添加的项目,其他项目将显示“默认”.有人知道发生了什么吗?真的很奇怪谢谢!
这是代码:
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 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); }解决方法:
感谢@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); } 总结 以上是内存溢出为你收集整理的android-以编程方式将视图添加到LinearLayout不会在除一个以外的所有内容上设置文本全部内容,希望文章能够帮你解决android-以编程方式将视图添加到LinearLayout不会在除一个以外的所有内容上设置文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)