2013-11-28 37 views
0

假设我在RelativeLayout中有两个按钮。顶部标有“one”的按钮和“one”下面标有“三”的按钮。布局是这样定义的。 RelativeLayout在两个人之间插入视图

<RelativeLayout 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:id="@+id/mainContainer" 
    tools:context=".MainActivity" > 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tvOne" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentTop="true" 
     android:text="One" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tvThree" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/tvOne" 
     android:text="Three" /> 
</RelativeLayout> 

所以我写了一些代码的MainActivityonCreate动态创建Button,以及一个和三个之间插入进去。但它不起作用。有什么我失踪?我将这个问题作为我遇到的更大问题的简化版本创建,因此我无法清除布局,只需动态插入一个二和三个。

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button one = (Button)findViewById(R.id.tvOne); 
    Button three = (Button)findViewById(R.id.tvThree); 

    //Dynamically create a button, set it underneath one and above two. 
    Button two = new Button(this); 
    two.setText("TWO TWO TWO TWO TWO TWO TWO"); 

    //Create some layout params so that this button is horizontally centered, 
    //above button number three and below button number one 
    final int WC = RelativeLayout.LayoutParams.WRAP_CONTENT; 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WC, WC); 
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
    params.addRule(RelativeLayout.BELOW, one.getId()); 
    params.addRule(RelativeLayout.ABOVE, three.getId()); 

    two.setLayoutParams(params); 

    //Add button number two to the activity. 
    RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainContainer); 
    rl.addView(two); 
} 
+0

其中第三个按钮让现在加入.. –

+0

你不改变三个按钮的LayoutParams ..所以它总是有效的“机器人:layout_below =” @ ID/tvOne“” 。 – fasteque

+0

可能这可以对你有所帮助: http://stackoverflow.com/questions/12524589/how-can-we-add-buttons-at-dynamic-positions-in-layout –

回答

3

工作代码和我已经检查过这个。

public class Main extends Activity { 

    Context ctx; 
    RelativeLayout rlayMainContainer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ctx = this; 

     rlayMainContainer = (RelativeLayout) findViewById(R.id.mainContainer); 
     Button one = (Button) findViewById(R.id.tvOne); 
     Button three = (Button) findViewById(R.id.tvThree); 


     // adding button two dynamically 
     Button two = new Button(ctx); 
     two.setText("hello"); 
     two.setId(12); 

     RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     lpSecond.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     lpSecond.addRule(RelativeLayout.BELOW, one.getId()); 

     rlayMainContainer.addView(two, lpSecond); 

     //align button three below button two 

     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) three 
       .getLayoutParams(); 

     params.addRule(RelativeLayout.BELOW, two.getId()); 

     three.setLayoutParams(params); 
    } 
} 

enter image description here

+0

是的这个作品!感谢帮助 –

0

事实上,“两化”按钮是存在的,但你不能看到它,因为它的高度= 0 这些代码行做“两化”按钮的高度= 0

params.addRule(RelativeLayout.BELOW, one.getId()); 
params.addRule(RelativeLayout.ABOVE, three.getId()); 

是的,“two”按钮layoutParams表示它必须介于“one”和“three”之间,但这些按钮之间没有空间 - >没有高度。

为了解决这个问题,你需要删除设置“两个”高于“三”行,并添加代码,表明“三”现在下面的“二”

 final int WC = RelativeLayout.LayoutParams.WRAP_CONTENT; 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
       WC, WC); 
     params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
     params.addRule(RelativeLayout.BELOW, one.getId()); 

     two.setLayoutParams(params); 

     // Add button number two to the activity. 
     RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainContainer); 
     rl.addView(two); 

     two.setId(1); 
     params = (LayoutParams) three.getLayoutParams(); 
     params.addRule(RelativeLayout.BELOW, two.getId()); 
     three.setLayoutParams(params); 
+0

现在我得到一个我无法解决的循环参考问题。 –

+0

哎呀,对不起,正如我上面评论:删除设置“两个”在“三”以上的线。 (我编辑了代码) –

相关问题