2012-02-17 60 views
0

我創造了這個佈局:如何在另一個LinearLayout之後添加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> 

我wanto點擊在alertDialog「添加水平」(每級中後以編程方式添加LinearLayout.ly_level在LinearLayout.ly_create_structure 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後添加。你能幫我實施嗎?

UPDATE 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); 
    } 

UPDATE 2: 我解決了這個問題。餘移動下面的代碼中的ShowDialog()

ViewGroup vg = (ViewGroup) ib.getParent(); 
int index = mainActivityLayout.indexOfChild(vg); 

回答

0

可以在Dev 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); 
    } 
+0

我在原始消息 – Webman 2012-02-17 13:29:55

0

好,所有的 首先,你會得到視圖對象,然後創建的LinearLayout對象,你是做在我們當前的代碼,並添加viewObject.addView(「的LinearLayout對象」);.我認爲,這是對你有幫助..

+0

增加了一個更新也許我不明白..但我想要一個基於LinearLayout索引的解決方案 – Webman 2012-02-17 14:26:58

相關問題