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

Java RelativeLayout在其他两个视图之间插入视图,java,android,android-layout,Java,Android,Android Layout,假设我在一个RelativeLayout中有两个按钮。顶部有一个标有“一”的按钮,底部有一个标有“三”的按钮。布局是这样定义的。 事实上,“两个”按钮在那里,但是你看不到它,因为它的高度=0。 这些代码行使“两个”按钮的高度为0 params.addRule(RelativeLayout.BELOW, one.getId()); params.addRule(RelativeLayout.ABOVE, three.getId()); 是的,“两个”按钮布局参数表示它必须介于“一”和“三”之间

假设我在一个
RelativeLayout
中有两个按钮。顶部有一个标有“一”的按钮,底部有一个标有“三”的按钮。布局是这样定义的。

事实上,“两个”按钮在那里,但是你看不到它,因为它的高度=0。 这些代码行使“两个”按钮的高度为0

params.addRule(RelativeLayout.BELOW, one.getId());
params.addRule(RelativeLayout.ABOVE, three.getId());
是的,“两个”按钮布局参数表示它必须介于“一”和“三”之间,但这些按钮之间没有空间-->没有高度

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

事实上,“两个”按钮在那里,但是你看不到它,因为它的高度=0。 这些代码行使“两个”按钮的高度为0

params.addRule(RelativeLayout.BELOW, one.getId());
params.addRule(RelativeLayout.ABOVE, three.getId());
是的,“两个”按钮布局参数表示它必须介于“一”和“三”之间,但这些按钮之间没有空间-->没有高度

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

事实上,“两个”按钮在那里,但是你看不到它,因为它的高度=0。 这些代码行使“两个”按钮的高度为0

params.addRule(RelativeLayout.BELOW, one.getId());
params.addRule(RelativeLayout.ABOVE, three.getId());
是的,“两个”按钮布局参数表示它必须介于“一”和“三”之间,但这些按钮之间没有空间-->没有高度

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

事实上,“两个”按钮在那里,但是你看不到它,因为它的高度=0。 这些代码行使“两个”按钮的高度为0

params.addRule(RelativeLayout.BELOW, one.getId());
params.addRule(RelativeLayout.ABOVE, three.getId());
是的,“两个”按钮布局参数表示它必须介于“一”和“三”之间,但这些按钮之间没有空间-->没有高度

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


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

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

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

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

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

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

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

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


第三个按钮现在在哪里添加..您不会更改按钮三的layoutparams。。因此,下面的“android:layout_=“@id/tvOne”总是有效的。这可能会对您有所帮助:第三个按钮现在添加到哪里..您不会更改按钮三的layoutparams。。因此,下面的“android:layout_=“@id/tvOne”总是有效的。这可能会对您有所帮助:第三个按钮现在添加到哪里..您不会更改按钮三的layoutparams。。因此,下面的“android:layout_=“@id/tvOne”总是有效的。这可能会对您有所帮助:第三个按钮现在添加到哪里..您不会更改按钮三的layoutparams。。所以“android:layout_below=“@id/tvOne”总是有效的。这可能对你有帮助:现在我遇到了一个循环引用问题,我无法解决。哦,对不起,正如我上面的评论:删除设置“2”在“3”之上的行。(我已经编辑了代码)现在我遇到了一个循环引用问题,我无法解决。哦,对不起,正如我上面的评论:删除设置“2”在“3”之上的行。(我已经编辑了代码)现在我遇到了一个循环引用问题,我无法解决。哦,对不起,正如我上面的评论:删除设置“2”在“3”之上的行。(我已经编辑了代码)现在我遇到了一个循环引用问题,我无法解决。哦,对不起,正如我上面的评论:删除设置“2”在“3”之上的行。(我已经编辑了代码)