如何以编程方式调整RelativeLayout中LinearLayout的宽度?

时间:2014-01-01 20:49:14

标签: android android-layout nested

我有一个主要的RelativeLayout对象,我想在其中添加一个LinearLayout对象,但也可以通过设置LinearLayout的LayoutParams,其中的元素,对齐到左边,而我希望最后一个元素附加在右边窗户的尽头。

这是我的第一个测试代码:

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout mainLayout = new RelativeLayout(this);
    mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    setContentView(mainLayout);

    LinearLayout Bottom = new LinearLayout(this);

    Button scale = new Button(this);
    scale.setText("Scale");

    Button attach = new Button(this);
    attach.setText("Attacch");

    TextView text = new TextView(this);
    text.setText("Something");

    Bottom.addView(scale);
    Bottom.addView(text);
    Bottom.addView(attach);

    mainLayout.addView(Bottom);

}
}

这就是结果:fist test code screenshot

如果我以这种方式编辑代码:

public class MainActivity extends Activity {    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar= getActionBar();

    RelativeLayout mainLayout = new RelativeLayout(this);
    mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    setContentView(mainLayout);

    LinearLayout Bottom = new LinearLayout(this);

    Button scale = new Button(this);
    scale.setText("Scale");

    Button attach = new Button(this);
    attach.setText("Attacch");
    attach.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

    TextView text = new TextView(this);
    text.setText("Something");
    text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

    Bottom.addView(scale);
    Bottom.addView(text);
    Bottom.addView(attach);

    mainLayout.addView(Bottom);


}}

我明白了:second test code screenshot

你可以看到attacch按钮消失......

简而言之,我想要中心文字并将附加按钮对齐

1 个答案:

答案 0 :(得分:0)

// try this way
@Override
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        RelativeLayout mainLayout = new RelativeLayout(this);

        LinearLayout Bottom = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;

        Button scale = new Button(this);
        LinearLayout.LayoutParams scaleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        scaleParams.gravity=Gravity.CENTER;
        scale.setText("Scale");

        Button attach = new Button(this);
        LinearLayout.LayoutParams attachParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        attachParams.gravity=Gravity.CENTER;
        attach.setText("Attacch");

        TextView text = new TextView(this);
        LinearLayout.LayoutParams textParams  = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
        textParams.gravity=Gravity.CENTER;
        text.setText("Something");

        Bottom.addView(scale,scaleParams);
        Bottom.addView(text,textParams);
        Bottom.addView(attach,attachParams);

        mainLayout.addView(Bottom,layoutParams);
        setContentView(mainLayout,new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT));
    }