How to make RelativeLayout not take up the whole screen?

I currently have a relative layout defined programmatically

RelativeLayout layout = new RelativeLayout(this); 

then i add text images and buttons like this

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

However, it makes the whole screen fill up with my relativity, how do I (programmatically) set only a certain number of pixels (dp) while occupying the screen? My goal is to have both the lower half of the screen relativelayout

Anyhelp?

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

I think this (in XML) can help me

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

I tried converting it to java like this before adding text images:

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

EDIT 2 I tried to add:

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

Not lucky yet

0
source share
3 answers

Check out the Android white papers 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 the TextView / EditText and Button (as in your example).

Here is the API link in Android white papers.

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 documentation, all links are there. :)

0
source

You did not provide a complete code sample, so I assume that your new RelativeLayout is a representation of the contents of your activity.

 setContentView( layout ); 

This means that your parent view is the layout of the frame, and you need to create FrameLayout.LayoutParams . This type of parameter does not give you many options, only gravity . You can try the following:

 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 is rather inconvenient. I think it’s easier to just insert an empty View into the top half of your RelativeLayout . It will still be fullscreen, but will no longer look fullscreen.

0
source

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

alternatively, you can set only the bottom (which can be any layout) to have gravity β€œfrom below” inside the window itself.

In fact, I recently asked about creating a dialog with a transparent background not so long ago, so this may be useful for you. here is the link . in my opinion, a very similar approach can be made for activities.

0
source

Source: https://habr.com/ru/post/1494752/


All Articles