简体   繁体   中英

How to set the height of a child view of a relative layout as a percentage of the total screen height

I have a main layout that I make relative so I can set a view to align at the bottom of the layout. The problem is that I want to set the width of the view as a percentage using layout gravity, but I am unable to do this as relative layouts do not support layout gravity.

How can I set the height of a child view of a relative layout as a percentage of the total screen height?

edit

I already tried using linear layout, but I cannot use android:layout_alignParentBottom="true" with a linear layout. The whole point is that the view needs to be at the bottom of the screen, but with the height as a percentage which I can't do with lay out gravity. It's a catch 22. What is the solution? Resort to programmatic settings?

<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=".MainActivity"
android:id="@+id/activity_main"
android:orientation="vertical">

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<View android:id="@+id/rectangle_at_the_bottom"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#00FFFF"
    android:layout_alignParentBottom="true" />

</RelativeLayout>

使用LinearLayout代替RelativeLayout并根据需要分配权重。

Please Check the following Layout File as I created one in my Screen:

<LinearLayout 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:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <!-- The view I want to set the height as a percentage -->

    <View
        android:id="@+id/rectangle_at_the_top"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#00FFFF" />

    <!-- My list view -->

</RelativeLayout>

Replace

<View android:id="@+id/rectangle_at_the_top"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#00FFFF"
    android:layout_alignParentBottom="true"/>

with

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/listView1"
    android:orientation="vertical"
    android:weightSum="1">
       <View android:id="@+id/rectangle_at_the_top"
         android:layout_width="match_parent"
         android:background="#00FFFF"
         android:layout_height="0dp"
         android:layout_weight="0.5" />
</LinearLayout>

Here, I have assumed you want to fill 50% of the remaining space, and hence put layout_weight as 0.5. You may replace it with your required percentage.

I figured out the solution on my own.

<LinearLayout 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=".MainActivity"
    android:id="@+id/activity_main"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_alignParentBottom="true"
        android:layout_above="@id/listView1">

        <View android:id="@+id/rectangle_at_the_bottom"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00FFFF" />
    </LinearLayout>

</LinearLayout>

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