RelativeLayout在其他两个视图之间插入一个视图 [英] RelativeLayout insert a view between two others

查看:57
本文介绍了RelativeLayout在其他两个视图之间插入一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say I have two buttons in a RelativeLayout. A button labeled "one" at the top and a button labeled "three" underneath "one". The layout is defined like this.

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

因此,我在 MainActivity onCreate 中编写了一些代码,以动态创建 Button ,并将其插入到一到三个之间.但这不起作用.有什么我想念的吗?我将这个问题创建为我遇到的一个较大问题的简化版本,因此我无法清除布局并仅动态插入一二和三.

So I wrote some code in the onCreate of MainActivity to dynamically create a Button, and insert it in between one and three. But it isn't working. Is there something I'm missing? I created this question as a simplified version of a bigger problem I'm having, so it's not acceptable for me to clear the layout and just insert one two and three dynamically.

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

推荐答案

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

Working code and I have checked this.

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

这篇关于RelativeLayout在其他两个视图之间插入一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆