Android makes 1/3 button width and height of the screen in relative layout

I have XML. This XML has one button inside the relative layout. The relative layout is full screen. I want the button to have 1/3 of the width and height of the screen. How can i do this?

Here is the XML:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="0dp" android:paddingRight="0dp" android:paddingTop="0dp" android:paddingBottom="0dp" tools:context="com.vroy.trapper.MainMenuActivity" android:layout_weight="2"> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="@string/menu_button_text" android:background="@drawable/round_button" android:id="@+id/button" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 

I looked at this question, but even though I assigned weight to the relative layout, I could not assign weight to the button.

+5
source share
4 answers

you can do it easily in java code.

write this code in oncreate.

  Button b=(Button) findViewById(R.id.button); int width = getResources().getDisplayMetrics().widthPixels/3; int hei=getResources().getDisplayMetrics().heightPixels/3; b.setLayoutParams(new RelativeLayout.LayoutParams(width,hei)); 
+9
source

The percent support library is what you need.

This library is pretty easy to use, since it's the same RelativeLayout and FrameLayout that we are familiar with, just with some extra features.

First of all, since the Percent Support Library comes with Android Support Library 23, please make sure that you update the Android Support Library in the SDK Manager to the latest version. Then add the dependency as shown below in the build.gradle file:

compile 'com.android.support:percent:23.0.0'

Now back to your Question, you can do something similar to achieve your result. Hope this helps you.

 <?xml version="1.0" encoding="utf-8"?> <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/bird" android:text="Your text" app:layout_heightPercent="33%" app:layout_widthPercent="33%" /> </android.support.percent.PercentRelativeLayout> 
+4
source

(EDITED will also consider width) Wrap the button around the line layout.

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="0dp" android:paddingLeft="0dp" android:paddingRight="0dp" android:paddingTop="0dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center" android:weightSum="3" > <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:weightSum="3"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_weight="1" android:text="@string/menu_button_text" android:background="@drawable/round_button" /> </LinearLayout> </LinearLayout> </RelativeLayout> 

Change android:gravity if you want your button to be right / left, etc. However, you can still use your relative layout for other things around it.

+2
source

Use LinearLayout instead of RelativeLayout to achieve your goal.

  <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <View android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <View android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="@string/menu_button_text" android:background="@drawable/round_button" android:id="@+id/button"/> <View android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> 

Hope this helps!

0
source

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


All Articles