2013-05-30 83 views
0

我怎樣才能以編程方式添加新的EditText到RelativeLayout,這是在LinearLayout?Android的更新RelativeLayout這是在LinearLayout

我的佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@color/bg" 
    android:id="@+id/mainlayout" 
    tools:context=".NwActivity" android:baselineAligned="false"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/rlA" > 

     <TextView 
      android:id="@+id/teamA" 
      android:text="@string/teamA" 
      android:textSize="24sp" 
      style="@style/Txt" /> 

     <Button 
      android:id="@+id/pAddA" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/teamA" 
      style="@style/Txt" 
      android:text="@string/pAdd" 
      android:onClick="addA" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/rlB" > 

     <TextView 
      android:id="@+id/teamB" 
      android:text="@string/teamB" 
      android:textSize="24sp" 
      style="@style/Txt" /> 

     <Button 
      android:id="@+id/pAddB" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/teamB" 
      style="@style/Txt" 
      android:text="@string/pAdd" 
      android:onClick="addB" /> 

    </RelativeLayout> 

</LinearLayout> 

我的活動:

package basketball.counter; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.LinearLayout; 

public class NwActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_nw); 
    } 

    public void addA(View view) { 
     LinearLayout ll = (LinearLayout) findViewById(R.id.mainlayout); 
     EditText et = new EditText(this); 
     et.setHint(R.string.pHint); 
     ll.addView(et); 
     setContentView(ll); 
    } 

    public void addB(View view) { 

    } 

} 

我只得到它的mainLayout。我也需要在EditText參數上設置「LayoutBelow」。謝謝。

回答

1

試試這個在這整個的EditText添加在運行時,你可以通過XML添加此給予ID

 RelativeLayout rl=(RelativeLayout)findViewById(R.id.relativelayoutid);  

    Edit Text et=new EditText(this); 
    et.setHeight(50); 
    et.setWidth(50); 
    et.setId(1); 



    EditText et1=new EditText(this); 
    et1.setHeight(50); 
    et1.setWidth(50); 
    et1.setId(3); 

      RelativeLayout.LayoutParams params1= new RelativeLayout.LayoutParams 
     (LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT); 



     params1.addRule(RelativeLayout.BELOW,et.getId()); 
     params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     et1.setLayoutParams(params1); 
      rl.addView(et1); 

希望這將幫助你

+0

工作得很好!謝謝 :) – user2350530

相關問題