How To Arrange Width And Height Of Relative Layout To Be Half Of The Screen
I am developing an android application and I want to set my relative layout's width and height as half of my screen width and height. Thanks
Solution 1:
Use Parent as the LinearLayout
and use weightSum
and weight
attributes
Sample
<LinearLayoutxmlns: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:orientation="vertical"android:weightSum="100" ><RelativeLayoutandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="50" ><!-- Your RelativeLayout Items --></RelativeLayout></LinearLayout>
Solution 2:
To Divide a Relative layout into two equal relative layouts
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:baselineAligned="false"><RelativeLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"></RelativeLayout><RelativeLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"></RelativeLayout></RelativeLayout>
and to Divide a linear layout into two equal relative layouts
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:baselineAligned="false"><RelativeLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"></RelativeLayout><RelativeLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"></RelativeLayout></LinearLayout>
Solution 3:
*@SuppressLint("NewApi")publicstaticintgetDeviceWidth(Activity activity) {
intdeviceWidth=0;
Pointsize=newPoint();
WindowManagerwindowManager= activity.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(size);
deviceWidth = size.x;
} else {
Displaydisplay= windowManager.getDefaultDisplay();
deviceWidth = display.getWidth();
}
return deviceWidth;
}
@SuppressLint("NewApi")publicstaticintgetDeviceHeight(Activity activity) {
intdeviceHeight=0;
Pointsize=newPoint();
WindowManagerwindowManager= activity.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(size);
deviceHeight = size.y;
} else {
Displaydisplay= windowManager.getDefaultDisplay();
deviceHeight = display.getHeight();
}
return deviceHeight;
}*
above two function is get device height and width.
Use on this function, you can get half of the size of screen. I hope its useful to you.
Solution 4:
You need to put the RelativeLayout
inside a LinearLayout
and use android:weightsum=2
and android:layout_weight=1
to get the desired proportion
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:weightSum="2" ><RelativeLayout....android:layout_weight=1....></RelativeLAyout></LinearLayout>
Post a Comment for "How To Arrange Width And Height Of Relative Layout To Be Half Of The Screen"