![](/img/trans.png)
[英]How to create LinearLayout with ImageView and add it to another LinearLayout programatically
[英]How to add a LinearLayout after another LinearLayout?
我創建了這個布局:
<!-- create_structure.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ly_create_structure"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
還有這個
<!-- add_level_in_structure.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ly_level"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1#"
android:layout_weight="1"/>
<EditText
android:id="@+id/edit_sb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:layout_weight="3">
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/add_level"
android:scaleType="fitXY"
android:layout_width="48dip"
android:layout_height="48dip"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="@drawable/ic_menu_add" />
</LinearLayout>
我想在alertDialog中單擊“添加級別”后,以編程方式在LinearLayout.ly_create_structure中添加LinearLayout.ly_level(LinearLayout.ly_level中的每個級別都有一個鏈接,以顯示一個alertDialog)。
代碼是:
public class CreateStructureActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_structure);
addLevel();
}
private void addLevel() {
LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.ly_create_structure);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ly = (LinearLayout) li.inflate(R.create_structure, null);
ImageButton ib = (ImageButton) ly.findViewById(R.id.add_level);
ib.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
mainActivityLayout.addView(ly);
}
private void showDialog() {
final CharSequence[] items = {"Add Level", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Actions");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0:
addLivello();
break;
case 1:
delete();
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
使用此代碼,我可以最后添加LinearLayout,但是我想在包含單擊顯示AlertDialog的按鈕的LinearLayout之后添加它。 你能幫我實現這個嗎?
更新1:我不明白如何獲取索引。 我以這種方式嘗試:
private void addLevel() {
LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.ly_create_structure);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ly = (LinearLayout) li.inflate(R.create_structure, null);
ImageButton ib = (ImageButton) ly.findViewById(R.id.add_level);
/**
* New code to get index
*/
ViewGroup vg = (ViewGroup) ib.getParent();
int index = mainActivityLayout.indexOfChild(vg);
ib.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
mainActivityLayout.addView(ly, index);
}
更新2:我解決了問題。 我在showDialog()中移動下面的代碼
ViewGroup vg = (ViewGroup) ib.getParent();
int index = mainActivityLayout.indexOfChild(vg);
您可以在Android開發人員版上使用方法
這也需要參數的索引。
private void addLevel(int index) {
LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.ly_create_structure);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ly = (LinearLayout) li.inflate(R.create_structure, null);
ImageButton ib = (ImageButton) ly.findViewById(R.id.add_level);
ib.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
mainActivityLayout.addView(ly, index);
}
好的,首先,您將獲得視圖對象,然后像在當前代碼中那樣創建linearlayout對象,並添加viewObject.addView(“ linearlayout object”);。 我認為,這對您有很大幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.