简体   繁体   中英

How can i get my Button on the Bottom Left?

i am trying to get the Button to the Bottom-Left. Why won't this work ? I tried for 2 hours and couldn't achieve it. :(

    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)); 

You'll need to add layout parameters to the button instead of gravity.

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 );

Why aren't you using XML layouts anyway? They are generally easier to understand and make.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM