0

我试图让按钮位于左下角。为什么这行不通?我尝试了 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)); 
4

2 回答 2

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 );
于 2012-11-05T23:32:29.217 回答
1

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

于 2012-11-05T23:33:33.520 回答