【问题标题】:Setting ScrollView weight makes View not shown设置 ScrollView 权重使 View 不显示
【发布时间】:2015-06-30 17:19:01
【问题描述】:

我正在尝试将 50% 的高度设置为 ScrollView。为此,我使用了android:layout_height="0dp"android:layout_weight="0.5",但未显示ScrollView 和LinearLayout。这是我的.xml

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

    <TextView       
        android:id="@+id/registerProductTitle"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="20dip"
        android:layout_marginBottom="20dip"
        android:gravity="center"
        android:text="@string/registerProductTitle"
        android:textSize="12pt">
    </TextView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp" // 50% height
        android:layout_weight="0.5" // 50% height
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip">

        <TableLayout
            android:id="@+id/mainTable"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:paddingLeft="3dip"
            android:paddingRight="3dip"
            android:shrinkColumns="1"
            android:stretchColumns="*">

            <TableRow>
                <TextView
                    android:id="@+id/productLabel"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="@string/productLabel"
                    android:textSize="10pt">
                </TextView>

                <EditText
                    android:id="@+id/productName"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:hint="@string/productTextHolder"
                    android:inputType="textPersonName" >
                </EditText>
            </TableRow>         
        </TableLayout> 
    </ScrollView>       

    <LinearLayout // This Layout is not being displayed
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/buttonSaveProduct"
            style="?android:attr/buttonStyleSmall"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:layout_gravity="bottom"
            android:text="@string/saveProduct" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-layout android-scrollview android-layout-weight


    【解决方案1】:

    如果您为 LinearLayout 的子项使用 weight 属性,请确保为每个子项定义它,否则您可以获得有趣的输出。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout    
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    
        <TextView       
            android:id="@+id/registerProductTitle"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginTop="20dip"
            android:layout_marginBottom="20dip"
            android:gravity="center"
            android:text="@string/registerProductTitle"
            android:layout_weight="0.25"
            android:textSize="12pt">
        </TextView>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip">
    
            <TableLayout
                android:id="@+id/mainTable"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:paddingLeft="3dip"
                android:paddingRight="3dip"
                android:shrinkColumns="1"
                android:stretchColumns="*">
    
                <TableRow>
                    <TextView
                        android:id="@+id/productLabel"
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:text="@string/productLabel"
                        android:textSize="10pt">
                    </TextView>
    
                    <EditText
                        android:id="@+id/productName"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:hint="@string/productTextHolder"
                        android:inputType="textPersonName" >
                    </EditText>
                </TableRow>         
            </TableLayout> 
        </ScrollView>       
    
        <LinearLayout
            android:layout_weight="0.5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1"
            android:gravity="bottom"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/buttonSaveProduct"
                style="?android:attr/buttonStyleSmall"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_width="0dip"
                android:layout_weight="1"
                android:layout_height="50dp"
                android:layout_gravity="bottom"
                android:text="@string/saveProduct" />
        </LinearLayout>
    </LinearLayout>
    

    我为下面的 LinearLayout 和上面的 TextView 添加了权重。

    在此处查看更多信息:Android Developers | LinerLayout

    【讨论】:

      【解决方案2】:

      当您在代码中重新设置视图的权重时,您应该重新设置其他视图的 LinerLayout 的权重,例如滚动视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-09
        • 2015-11-14
        相关资源
        最近更新 更多