我想把按钮放到左下角。为什么这不起作用?我尝试了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));
答案 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布局?它们通常更容易理解和制作。