将两个布局放在一起

时间:2016-04-14 09:39:00

标签: android android-layout android-linearlayout

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@drawable/ightwall"
    android:id="@+id/drawerlayout"
    >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#6CEB87"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="my list"/>
        </LinearLayout>

    </ScrollView>

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

        <ImageView
            android:id="@+id/mylist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/createlist_mylist"
            android:adjustViewBounds="true"
            android:maxHeight="100dp"
            android:maxWidth="100dp"/>

        <ImageView
        android:id="@+id/freeadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/createlist_freetext"
        android:adjustViewBounds="true"
        android:maxHeight="100dp"
        android:maxWidth="100dp"/>

        <ImageView
            android:id="@+id/categoryadd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/createlist_categorysearch"
            android:adjustViewBounds="true"
            android:maxHeight="100dp"
            android:maxWidth="100dp"/>

        <ImageView
            android:id="@+id/favproductadd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/createlist_favproduct"
            android:adjustViewBounds="true"
            android:maxHeight="100dp"
            android:maxWidth="100dp"/>



    </LinearLayout>
</LinearLayout>

我希望两个线性图层看起来像这样:

the red layout will be for buttons ,and the green layout will be for changeable screens

我正在开发android studio。 我需要红色布局来填充绿色布局的固定宽度以填充屏幕的其余部分。

i want to get rid of the empty space i marked in red

2 个答案:

答案 0 :(得分:0)

android:layout_weight="1"添加到第二个LinearLayout,并将宽度设置为0

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@drawable/ightwall"
    android:id="@+id/drawerlayout"
    >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">

        ...

    </ScrollView>

    <LinearLayout
        android:layout_width="0dp" // change
        android:layout_weight="1" // add layout_weight
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#D5D4D4">

        ...

    </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

在主布局中设置android:weightSum="1"并将android:layout_weightlayout_width should be 0dp when you set layout_weight)提供给其他两个布局

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawerlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:weightSum="1" >

        <ScrollView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:layout_weight=".7" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="#6CEB87"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="my list" />
            </LinearLayout>
        </ScrollView>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
android:weightSum="4"
            android:layout_gravity="right"
            android:layout_weight=".3"
            android:background="#D5D4D4"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/mylist"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
            android:layout_weight="1"
                android:adjustViewBounds="true"
                android:maxHeight="100dp"
                android:maxWidth="100dp"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:id="@+id/freeadd"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
            android:layout_weight="1"
                android:adjustViewBounds="true"
                android:maxHeight="100dp"
                android:maxWidth="100dp"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:id="@+id/categoryadd"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
            android:layout_weight="1"
                android:adjustViewBounds="true"
                android:maxHeight="100dp"
                android:maxWidth="100dp"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:id="@+id/favproductadd"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
            android:layout_weight="1"
                android:adjustViewBounds="true"
                android:maxHeight="100dp"
                android:maxWidth="100dp"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>

    </LinearLayout>
相关问题