简体   繁体   中英

How to distribute space between a parent element and a child viewgroup

I would like to build a layout with an image that should be 90 percent height of the screen and the last 10% is the 3 text views that i have in a horizontal layout im able to acheive this using setting the height of the image in dp to cover 90 percent of the screen(500dp) i tried layout_weight everywhere and it aintworking is there any way i could acheive this without explicitly setting the height of the image Hers a screenshot! This is my end goal but i have explicitly set image height in this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical"
    android:weightSum="2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/martin" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >

        <TextView
            android:text="Martin garrix"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
             />

        <TextView
            android:text="Beleive"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
             />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Imagine!!" />
    </LinearLayout>
</LinearLayout>

You have to use android:height="0dp" and android:layout_height="9" for you image and android:layout_height="1" for the other container. Also the weightSum should be 10. You can check this for examples of layout_weigth http://abhiandroid.com/ui/linear-layout

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:orientation="vertical"
        android:weightSum="10" >

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="0dp"
            android:layout_weight="9"
            >

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/martin" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            >

            <TextView
                android:text="Martin garrix"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                  />

            <TextView
                android:text="Beleive"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                 />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Imagine!!" />
        </LinearLayout>
    </LinearLayout>

You can simply achieve this without using to multiple time weight in 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="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/descripcion_actividad"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/layout"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher"

        />

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/hora_inicio"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/descripcion_actividad"
            android:layout_alignParentBottom="false"
            android:layout_alignStart="@+id/descripcion_actividad"
            android:layout_below="@+id/descripcion_actividad"
            android:layout_weight="1"
            android:text="TextView" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/descripcion_actividad"
            android:layout_alignParentBottom="false"
            android:layout_alignStart="@+id/descripcion_actividad"
            android:layout_below="@+id/descripcion_actividad"
            android:layout_weight="1"
            android:text="TextView" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/descripcion_actividad"
            android:layout_alignParentBottom="false"
            android:layout_alignStart="@+id/descripcion_actividad"
            android:layout_below="@+id/descripcion_actividad"
            android:layout_weight="1"
            android:text="TextView" />

    </LinearLayout>


</RelativeLayout>

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