繁体   English   中英

RelativeLayout在其他两个视图之间插入一个视图

[英]RelativeLayout insert a view between two others

假设我在RelativeLayout有两个按钮。 顶部的标有“一个”的按钮和下方的标有“ 3”的按钮。 布局是这样定义的。

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

工作代码,我已经检查过了。

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

在此处输入图片说明

实际上,“ two”按钮在那里,但因为高度= 0而看不到。这些代码行使“ two”按钮的高度= 0

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

是的,“两个”按钮layoutParams表示它必须在“一个”和“三个”之间,但是这些按钮之间没有空格->没有高度。

为了解决这个问题,您需要删除设置“ two”在“ three”之上的行,并添加代码以指示“ three”现在在“ two”之下。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM