开发者

Android stretch layout heights so that layout takes up 50% of screen

开发者 https://www.devze.com 2023-03-20 11:15 出处:网络
I have a LinearLayout that contains sublayouts which essentially mimick a 3x2 grid. Each sublayout takes up an equal amount of space across the screen (in width).Now, I\'m looking to have the two rows

I have a LinearLayout that contains sublayouts which essentially mimick a 3x2 grid. Each sublayout takes up an equal amount of space across the screen (in width). Now, I'm looking to have the two rows of sublayouts stretch in height so that each takes up 50% of the screen. I've tried playing around with the weights, however I don't know how to modify my existing layout code without messing up the existing weights in place used for the widths. Can anyone provide me with some help? Thanks! Here is my layout code:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_marginBottom="2dip"
        android:layout_height="wrap_content"    >

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_weight="50"
                android:padding="10dip"
                android:layout_marginRight="1dip"
                android:background="@drawable/detail_row"
                android:layout_height="wrap_content"    >

                    <ImageView
                        android:id="@+id/menuImage1"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/textImage1"
                        android:layout_column="1"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:text=""
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

            </LinearLayout>


            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_weight="50"
                android:padding="10dip"
                android:layout_marginLeft="1dip"
                android:layout_marginRight="1dip"
                android:background="@drawable/detail_row"
                android:layout_height="wrap_content"    >

                    <ImageView
                        android:id="@+id/menuImage2"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_width="wrap_content"
   开发者_如何转开发                     android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/textImage2"
                        android:layout_column="1"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:text=""
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_weight="50"
                android:padding="10dip"
                android:layout_marginLeft="1dip"
                android:background="@drawable/detail_row"
                android:layout_height="wrap_content"    >

                    <ImageView
                        android:id="@+id/menuImage3"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/textImage3"
                        android:layout_column="1"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:text=""
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

            </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    >

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_weight="50"
                android:padding="10dip"
                android:layout_marginRight="1dip"
                android:background="@drawable/detail_row"
                android:layout_height="wrap_content"    >

                    <ImageView
                        android:id="@+id/menuImage4"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/textImage4"
                        android:layout_column="1"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:text=""
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

            </LinearLayout>


            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_weight="50"
                android:padding="10dip"
                android:layout_marginLeft="1dip"
                android:layout_marginRight="1dip"
                android:background="@drawable/detail_row"
                android:layout_height="wrap_content"    >

                    <ImageView
                        android:id="@+id/menuImage5"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/textImage5"
                        android:layout_column="1"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:text=""
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

            </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_weight="50"
                android:padding="10dip"
                android:layout_marginLeft="1dip"
                android:background="@drawable/detail_row"
                android:layout_height="wrap_content"    >

                    <ImageView
                        android:id="@+id/menuImage6"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/textImage6"
                        android:layout_column="1"
                        android:layout_gravity="center_horizontal|center_vertical"
                        android:text=""
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

            </LinearLayout>

    </LinearLayout>


Apply equal android:layout_weight (for example 50) values on the two "main-parent" LinearLayouts in your existing code. This will make them each take half of any existing free space.

Then put all of your your code in another new LinearLayout. This new one should be set to fill-parent in both width and height. Its orientation should be vertical.

This should get you on the way but you will probably have to tweak your existing code to make it appear exactly as you want.

I run a short test using this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_marginBottom="2dip"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_marginBottom="2dip"
        android:layout_weight="50"
        android:layout_height="wrap_content">

        <LinearLayout android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="50" android:padding="10dip"
            android:layout_marginRight="1dip"
            android:background="@drawable/icon"
            android:layout_height="wrap_content">

            <ImageView android:id="@+id/menuImage1"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/textImage1"
                android:layout_column="1"
                android:layout_gravity="center_horizontal|center_vertical"
                android:text="" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>


        <LinearLayout android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="50" android:padding="10dip"
            android:layout_marginLeft="1dip"
            android:layout_marginRight="1dip"
            android:background="@drawable/icon"
            android:layout_height="wrap_content">

            <ImageView android:id="@+id/menuImage2"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/textImage2"
                android:layout_column="1"
                android:layout_gravity="center_horizontal|center_vertical"
                android:text="" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <LinearLayout android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="50" android:padding="10dip"
            android:layout_marginLeft="1dip"
            android:background="@drawable/icon"
            android:layout_height="wrap_content">

            <ImageView android:id="@+id/menuImage3"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/textImage3"
                android:layout_column="1"
                android:layout_gravity="center_horizontal|center_vertical"
                android:text="" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout android:orientation="horizontal"
        android:layout_weight="50"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="50" android:padding="10dip"
            android:layout_marginRight="1dip"
            android:background="@drawable/icon"
            android:layout_height="wrap_content">

            <ImageView android:id="@+id/menuImage4"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/textImage4"
                android:layout_column="1"
                android:layout_gravity="center_horizontal|center_vertical"
                android:text="" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>


        <LinearLayout android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="50" android:padding="10dip"
            android:layout_marginLeft="1dip"
            android:layout_marginRight="1dip"
            android:background="@drawable/icon"
            android:layout_height="wrap_content">

            <ImageView android:id="@+id/menuImage5"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/textImage5"
                android:layout_column="1"
                android:layout_gravity="center_horizontal|center_vertical"
                android:text="" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <LinearLayout android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="50" android:padding="10dip"
            android:layout_marginLeft="1dip"
            android:background="@drawable/icon"
            android:layout_height="wrap_content">

            <ImageView android:id="@+id/menuImage6"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/textImage6"
                android:layout_column="1"
                android:layout_gravity="center_horizontal|center_vertical"
                android:text="" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </LinearLayout>
</LinearLayout>


if you have two views each with layout_weight = 1 and layout_height = "fill_parent" then they should be sharing the screen space equally


Here is how I made it for me:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100">
    <LinearLayout android:layout_weight="50"
        android:layout_width="match_parent"
        android:id="@+id/linearLayout1"
        android:layout_height="wrap_content"
        android:orientation="vertical"></LinearLayout>
</LinearLayout>

http://www.hrupin.com/2012/02/how-to-set-view-width-height-in-percentage-of-parent-view-in-android

0

精彩评论

暂无评论...
验证码 换一张
取 消