我怎样才能让我的按钮在左下方?

时间:2012-11-05 23:18:10

标签: android relativelayout

我想把按钮放到左下角。为什么这不起作用?我尝试了2个小时,但无法实现。 :(

    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
            android.view.ViewGroup.LayoutParams.MATCH_PARENT);
    Button b = new Button(this);
    b.setText("take photo");
    b.setGravity(Gravity.BOTTOM | Gravity.LEFT);
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Select Area of Interest", Toast.LENGTH_SHORT).show();
        }
    });
    rl.addView(mGrabView,firstImageParams);
    rl.addView(b);
    rl.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
    this.addContentView(rl,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

2 个答案:

答案 0 :(得分:1)

您需要将布局参数添加到按钮而不是重力。

RelativeLayout rl = new RelativeLayout(this);

Button b = new Button( this );
b.setText( "take photo" );
b.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Select Area of Interest", Toast.LENGTH_SHORT).show();
    }
});

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM, true );
lp.addRule( RelativeLayout.ALIGN_PARENT_LEFT, true );
rl.addView( b, lp );

答案 1 :(得分:1)

为什么不使用XML布局?它们通常更容易理解和制作。