Adjust the width to half the screen

I came across this link:

Declare the width of the width to half the width of the screen

How to do the same in relative layout? Here is the code: Try linearlayout inside the relative layout. But that does not work.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button6" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_weight="1" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_weight="1" android:text="UID" android:textColor="#ffe5f1f1" /> <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ffe5f1f1" android:hint="Enter UID" android:inputType="number" android:shadowColor="5" android:textColor="#ff009999" android:textStyle="bold" android:typeface="monospace" > <requestFocus /> </EditText> <Button android:id="@+id/button7" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_alignParentLeft="true" android:layout_marginTop="18dp" android:text="Button" /> <Button android:id="@+id/button8" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button7" android:layout_weight="1" android:layout_marginTop="16dp" android:text="Button" /> <Button android:id="@+id/button9" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_alignParentLeft="true" android:layout_below="@+id/button8" android:layout_marginTop="17dp" android:text="Button" /> <EditText android:id="@+id/editText2" android:layout_width="50" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_weight="1" android:background="#ffe5f1f1" android:ems="10" android:hint="Enter Name" android:inputType="number" android:inputType="textPersonName" android:textColor="#ff009999" android:textStyle="bold" android:layout_weight="1" android:typeface="monospace" /> <EditText android:id="@+id/editText3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:ems="10" android:layout_weight="1" android:inputType="number" /> <EditText android:id="@+id/editText4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button7" android:layout_alignBottom="@+id/button7" android:layout_alignParentRight="true" android:ems="10" android:layout_weight="1" android:inputType="phone" /> <EditText android:id="@+id/editText5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button8" android:layout_alignBottom="@+id/button8" android:layout_alignParentRight="true" android:ems="10" android:layout_weight="1" android:inputType="textPersonName" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button9" android:layout_alignBottom="@+id/button9" android:layout_alignLeft="@+id/editText5" android:layout_weight="1" android:text="Male" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/radioButton2" android:layout_alignBottom="@+id/radioButton2" android:layout_alignParentRight="true" android:layout_weight="1" android:text="Female" /> <Button android:id="@+id/button10" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button9" android:layout_marginTop="31dp" android:layout_weight="1" android:text="Button" /> <Spinner android:id="@+id/spinDept" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button10" android:layout_alignBottom="@+id/button10" android:layout_alignLeft="@+id/radioButton2" android:layout_alignParentRight="true" android:layout_weight="1" android:background="#ff009999" /> </LinearLayout> </RelativeLayout> 
+6
source share
5 answers

This is a nice workaround if you need half when using RelativeLayout. What you do is make the fake invisible view oriented to the parent and leave the alignParentLeft and alignRight buttons for that fake view and the right button the other way around

 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <View android:id="@+id/fakeView" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerInParent="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/fakeView" android:layout_alignParentLeft="true" android:text="Left"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/fakeView" android:layout_alignParentRight="true" android:text="Right"/> </RelativeLayout> 

Its really smart.

+51
source

Wrap the same with a relative layout that should work for you.

 <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="somebutton"> <TextView android:layout_width="fill_parent" android:layout_height="Wrap_content" android:layout_weight="1"> </LinearLayout> 
+4
source

In addition to urSus answer, it is better to use Space instead of View :

  <Space android:id="@+id/fakeView" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerInParent="true"/> 
+2
source
  Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); plannerEditLayout = (RelativeLayout) findViewById(R.id.plannerLayoutEdit); plannerEditLayout.setGravity(Gravity.CENTER_VERTICAL); plannerEditLayout.setLayoutParams(new RelativeLayout.LayoutParams( (width / 2), height)); 

I use the same code for LinearLayout. Havent tried myslef for RelativeLayout. Give him a chance. Comment if it does not work.

+1
source
 <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="match_parent" android:id="@+id/button" android:layout_height="wrap_content" android:layout_weight="1" android:text="somebutton"> <TextView android:layout_width="match_parent" android:id="@+id/textview" android:layout_height="Wrap_content" android:layout_weight="1"> </LinearLayout> 

The above example uses LinearLayout, set the layout_width widget to match_parent and specify the weight (to indicate the area that will occupy).

This solves your problem.

-1
source

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


All Articles