android - 设置 ScrollView 权重使 View 不显示

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

我正在尝试将 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>

最佳答案

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

<?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

关于android - 设置 ScrollView 权重使 View 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24355226/

相关文章:

android - 查看按钮及其下方的列表 fragment

android-layout - 根据android应用程序中的自定义主题在自定义textviews之间切换

android - 如何防止Android ScrollView拦截其父点击事件

android - 如何使 RadioGroup 在 Android 中可滚动?

android - 如何将另一个 Layout 放在 Scrollview 之上

java - 使用自定义 XML 文件的 Android 滑动选项卡

android - Proguard 问题 "Warning:Ignoring InnerClasses attribute for an anonymous inner class"

android - 当另一个滚动时如何隐藏回收 View

javascript - com.facebook.react.common.JavascriptException : Requiring unknown module "./locale/ID". ,

android - 如何播放音频文件来调用?