简体   繁体   中英

How to make RelativeLayout not take up the whole screen?

Currently, I have a relative layout defined programatically

 RelativeLayout layout = new RelativeLayout(this);

then I add textviews and buttons like this

 layout.addView(myText, params);
 layout.addView(myBtn, param);

However, this makes the entire screen fill up with my relativelayout, how do I (programatically) set only certain number of pixels(dp) take up the screen? My goal is to have like the bottom half of the screen relativelayout

Anyhelp?

EDIT: This is what I am trying to achieve ultimately : http://i.imgur.com/n1ckBN7.png

I think this (in XML) might help me

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="200dp">

I tried converting it into java like this before I add the textviews:

 parameters_layout = new 
         RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
     200);
 layout.setLayoutParams(parameters_layout);

EDIT 2 I tried adding:

    parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    parameters_layout.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
    layout.setGravity(Gravity.BOTTOM);
    layout.setLayoutParams(parameters_layout);

Still no luck

Look on the official Android docs for RelativeLAyout.

http://developer.android.com/reference/android/widget/RelativeLayout.html

In particular,

setMinimumWidth(int)
setMinimumHeight(int)

Example:

layout.setMinimumWidth(500);
layout.setMinimumHeight(100);

If you want to set the height and width of TextView/EditText and Button (like in your example).

Here's the API reference in the official Android docs.

EditText: http://developer.android.com/reference/android/widget/EditText.html

setHeight(int)
setMaxHeight(int)
setMinHeight(int)
setMaxWidth(int)

Buttons: http://developer.android.com/reference/android/widget/Button.html

setHeight(int)
setMinWidth(int)

Just read the official documentations all of the reference are there. :)

You didn't give a full code sample, so I'm assuming that your new RelativeLayout is the content view of your activity.

setContentView( layout );

This means your parent view is a frame layout, and you need to create a FrameLayout.LayoutParams . That params type doesn't give you much options, only gravity . You could try this:

RelativeLayout layout = new RelativeLayout(this);
int halfScreenHeight = //calculate from DisplayMetrics
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(MATCH_PARENT, halfScreenHeight, Gravity.BOTTOM);
setContentView( layout, params );

...but it's quite awkward. I think it's easier to just insert an empty View in the top half of your RelativeLayout . It will still be full screen, but no longer look full screen.

according to what i see in the image, you can either put everything in a relativeLayout that covers everything, while the bottom views are aligned to the bottom (use align parent bottom for this).

alternatively, you can set only the bottom part (which can be any layout you wish) to have a gravity of "bottom" inside the window itself.

in fact, i've recently asked about creating a dialog with transparent background not so long ago, so it might be helpful for you. here's the link . from my understanding, a very similar approach can be done for activities.

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