在另一个xml文件中扩充布局时,权重不起作用

时间:2013-12-10 11:04:34

标签: android android-layout

Main.xml文件

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="100dp"
        android:layout_below="@+id/rel2" >



        <LinearLayout
            android:id="@+id/llData"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="8"
            android:orientation="vertical" >


        </LinearLayout>


    </ScrollView>

在这里,我将下面的xml文件添加到linearlayout llData ..

inflate_page.xml

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="3"
        android:weightSum="2"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imgImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/grid_default" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16dp"
                android:text="TextView" />

        </LinearLayout>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="4" 
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txtDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="TextView" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txtAddField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="TextView" />

    </LinearLayout>

使用以下代码在另一个中扩充布局:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View menuLayout = inflater.inflate(R.layout.inflate_page, llData,
                true);

下图是我想要的:

enter image description here

下图是我的实际输出:

enter image description here

请指导我如何解决这个体重问题。布局没有正确显示。

2 个答案:

答案 0 :(得分:1)

问题在于ScrollView。尝试添加到布局设置“fillViewport:true”

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="100dp"
        android:layout_below="@+id/rel2" 
        android:fillViewport="true" <---
        >

这样Scrollview将始终填满屏幕并解决您的问题。

答案 1 :(得分:0)

关注@Segi

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="3"
        android:weightSum="2"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imgImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16dp"
                android:text="TextView" />

        </LinearLayout>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="4" 
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txtDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="TextView" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txtAddField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="TextView" />

    </LinearLayout> 

</LinearLayout>