中心RelativeLayout或其内容

时间:2013-11-26 09:21:31

标签: android android-layout relativelayout

我正在制作一个小应用程序,因为我现在正在学习Android,我想出了一个小问题,我做了一个RelativeLayout(在一个java文件中,而不是xml文件中),然后在其中引入了一个相对于另一个。

最终布局如下:

http://i.stack.imgur.com/TlHUt.png

我所做的是创建中心顶行按钮,然后创建相对于他的所有其他按钮。我想做的是将RelativeLayout置于视图中心,这样我就可以将电路板置于中心位置,并且可以在屏幕顶部放置一些文本或其他内容。我考虑过在RelativeLayout之前创建一个LinearLayout,然后设置像margin这样的东西,或者将RelativeLAyout引入另一个布局以使其居中,但似乎不起作用,你建议我做什么来实现这个目标而不必改变我制作电路板的方式?以下是制作董事会的代码:

private RelativeLayout makeBoard (){
    int papi = 4;
    int grande =3;
    int peque = 1;
    RelativeLayout.LayoutParams params = new      RelativeLayout.LayoutParams(wrap_content,wrap_content);
    RelativeLayout parent = new RelativeLayout(this);


    parent.setLayoutParams(params);



    papi = makeRow(parent,papi,peque);
    papi = makeRow(parent,papi,peque);

    papi = makeRow(parent,papi,grande);
    papi = makeRow(parent,papi,grande);
    papi = makeRow(parent,papi,grande);

    papi = makeRow(parent,papi,peque);
    papi = makeRow(parent,papi,peque);
    return parent;
}

private int makeRow (RelativeLayout parent, int papasote,int colDer)
{


    int index = papasote+10;
    if (papasote==4)
    {
        createButtonSinMas (parent, index);
    }
    else
    {
        createButtonJustoAbajo(parent, index, papasote);
    }

    for (int i=0;i<colDer;i++)
    {
        createButtonAlaDeresha(parent, index+i+1, index+i);
        createButtonAlaIskierda(parent, index-i-1, index-i);
    }
    return index;   
}   


private int createButtonSinMas (RelativeLayout parent, int id)
{
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(SIZE, SIZE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);   
    CCCButton button = new CCCButton(this);
    button.setLayoutParams(params);
    return basura(parent,id,params, button);
}   

private int createButtonJustoAbajo (RelativeLayout parent, int id, int papasote)
{
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(SIZE, SIZE);
    params.addRule(RelativeLayout.BELOW, papasote);
    params.addRule(RelativeLayout.ALIGN_RIGHT, papasote);
    CCCButton button = new CCCButton(this);
    return basura(parent,id,params, button);

}

private int createButtonAlaDeresha (RelativeLayout parent, int id, int papasote)
{
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(SIZE, SIZE);
    params.addRule(RelativeLayout.RIGHT_OF, papasote);
    params.addRule(RelativeLayout.ALIGN_BASELINE,papasote);
    CCCButton button = new CCCButton(this);
    return basura(parent,id,params, button);
}   

private int createButtonAlaIskierda (RelativeLayout parent, int id, int papasote)
{
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(SIZE, SIZE);
    params.addRule(RelativeLayout.LEFT_OF, papasote);
    params.addRule(RelativeLayout.ALIGN_BASELINE,papasote);     
    CCCButton button = new CCCButton(this);
    return basura(parent,id,params, button);
}   

private int basura(RelativeLayout parent,int id,RelativeLayout.LayoutParams params, CCCButton button)
{
    button.setId(id);
    button.setLayoutParams(params);
    button.setOnClickListener(this);     
    actualizarBoton(button);

    parent.addView(button); 
    return button.getId();  
}

然后我就在主板上做了= makeBoard(); board是RelativeLayout变量。

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    game = new Game();
    board = makeBoard();
    setContentView(board);  
}

先谢谢你们

1 个答案:

答案 0 :(得分:0)

尝试在代码中进行以下更改。

将Layoutparams从wrap_content更改为match_parent

并将相对布局的重力设置为居中。

private RelativeLayout makeBoard() {
        int papi = 4;
        int grande = 3;
        int peque = 1;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        RelativeLayout parent = new RelativeLayout(this);

        parent.setLayoutParams(params);
        parent.setGravity(Gravity.CENTER);


        papi = makeRow(parent, papi, peque);
        papi = makeRow(parent, papi, peque);

        papi = makeRow(parent, papi, grande);
        papi = makeRow(parent, papi, grande);
        papi = makeRow(parent, papi, grande);

        papi = makeRow(parent, papi, peque);
        papi = makeRow(parent, papi, peque);
        return parent;
    }