我想给一个项目充气一次并循环使用它。我目前有一个解决方案,但很可能有更好的方法。此外,除非有调用,否则程序不会运行view.removeView
,这是有道理的,但如果我想catBtn
稍后在应用程序中添加,这似乎很危险)。
现有代码:
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
for(int i = 0; i < 10; ++i) {
LinearLayout assets = (LinearLayout)this.getLayoutInflater().inflate(R.layout.assets, null);
Button btn = (Button)assets.findViewById(R.id.catBtn);//new Button(this);
assets.removeView(btn);
col1.addView(btn);
}
现有的 layout.assets
<?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="match_parent"
android:weightSum="1"
android:id="@+id/assets">
<ImageView android:focusable="true"
android:id="@+id/thumb"
android:background="@drawable/selectable"
android:layout_marginBottom="20dip"
android:src="@drawable/icon" android:layout_height="140dip" android:layout_width="250dip"/>
<Button android:id="@+id/catBtn"
android:layout_height="wrap_content"
android:background="@drawable/selectable"
android:text="Cat Button"
android:layout_width="120dip"
android:textSize="16dip"></Button>
</LinearLayout>