如何在相对布局中一个接一个地拟合所有线性布局,以便在添加更多线性布局时调整其高度

时间:2016-09-08 11:27:44

标签: android xml android-layout user-interface layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="5"
                tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <ImageView
            android:id="@+id/im11"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

        <ImageView
            android:id="@+id/im12"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

        <ImageView
            android:id="@+id/im13"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />
        <ImageView
            android:id="@+id/im14"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout1"
        android:layout_weight="1"
        android:layout_centerHorizontal="true" >

        <ImageView
            android:id="@+id/im31"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

        <ImageView
            android:id="@+id/im32"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

        <ImageView
            android:id="@+id/im33"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />
        <ImageView
            android:id="@+id/im34"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

    </LinearLayout>
    <LinearLayout
        android:id="@+id/layout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout2"
        android:layout_centerHorizontal="true" >

        <ImageView
            android:id="@+id/im41"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

        <ImageView
            android:id="@+id/im42"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

        <ImageView
            android:id="@+id/im43"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />
        <ImageView
            android:id="@+id/im44"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

    </LinearLayout>
    <LinearLayout
        android:id="@+id/layout4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout3"
        android:layout_weight="1"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >

        <ImageView
            android:id="@+id/im51"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />

        <ImageView
            android:id="@+id/im52"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/munshee_logo" />

        <ImageView
            android:id="@+id/im53"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />
        <ImageView
            android:id="@+id/im54"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/munshee_logo" />


    </LinearLayout>

</RelativeLayout>

Here the images in each row are not of the same size, also I have 5 linear layouts in the code but only 3 are visible here

我希望linear layouts调整其高度,以便所有5 linear layouts适合屏幕。

3 个答案:

答案 0 :(得分:1)

  

您不需要Relative Layout作为Root Layout。因为您可以Linear Layout作为Root Layout执行此操作。使用Weight属性。

这是xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">


    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <ImageView
            android:id="@+id/im11"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im12"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im13"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im14"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <ImageView
            android:id="@+id/im31"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im32"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im33"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im34"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <ImageView
            android:id="@+id/im41"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im42"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im43"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im44"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <ImageView
            android:id="@+id/im51"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im52"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im53"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im54"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="4">

        <ImageView
            android:id="@+id/im1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"

            android:src="@drawable/common_ic_googleplayservices" />

        <ImageView
            android:id="@+id/im4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/common_ic_googleplayservices" />


    </LinearLayout>

</LinearLayout>

enter image description here

答案 1 :(得分:0)

尝试此代码....

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:weightSum="5"
   tools:context=".MainActivity">

   <LinearLayout
   android:id="@+id/layout1"
    android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_weight="1">

<ImageView
    android:id="@+id/im11"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

<ImageView
    android:id="@+id/im12"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

   <ImageView
    android:id="@+id/im13"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

   <ImageView
    android:id="@+id/im14"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />


    </LinearLayout>

   <LinearLayout
   android:id="@+id/layout2"
   android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_weight="1">
   <ImageView
    android:id="@+id/im31"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

    <ImageView
    android:id="@+id/im32"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"

    android:src="@mipmap/ic_launcher" />

    <ImageView
    android:id="@+id/im33"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

    <ImageView
    android:id="@+id/im34"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

    </LinearLayout>

    <LinearLayout
    android:id="@+id/layout3"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1">

   <ImageView
    android:id="@+id/im41"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

    <ImageView
    android:id="@+id/im42"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

   <ImageView
    android:id="@+id/im43"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

   <ImageView
    android:id="@+id/im44"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

    </LinearLayout>

    <LinearLayout
    android:id="@+id/layout4"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

   <ImageView
    android:id="@+id/im51"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

    <ImageView
    android:id="@+id/im52"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

    <ImageView
    android:id="@+id/im53"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />

    <ImageView
    android:id="@+id/im54"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:src="@mipmap/ic_launcher" />
   </LinearLayout>
 </LinearLayout>

答案 2 :(得分:0)

从相对和线性布局中删除所有android:layout_weight