【问题标题】:layout_weight giving unpredictable results on devicelayout_weight 在设备上给出不可预测的结果
【发布时间】:2014-12-04 12:58:10
【问题描述】:

我目前正在尝试组合一个由几个输入控件和一个列表视图组成的布局,并使用 layout_weight 在 Android Studio 预览中让它看起来像我想要的那样。但是,在实际设备上,布局看起来完全不同。

我对 Android 还很陌生,之前在他们的布局上遇到过一些问题,所以我可能遗漏了一些东西。但是,我一直在阅读不同类型的布局,尤其是 layout_weight,到目前为止我发现的所有内容都让我认为结果看起来像 Android Studio 的预览版。在这种情况下,我可以轻松地适应另一种布局,但我更愿意真正了解问题所在,因为我将来可能会再次遇到同样的问题。

以下是我的布局代码和比较图像。 非常感谢任何想法或提示。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <EditText
        android:id="@+id/input_title"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:hint="@string/title"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/input_submit"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:text="@string/input_submit"/>

        <Button
            android:id="@+id/input_date"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@id/input_submit"
            android:text="@string/input_date"/>

        <EditText
            android:id="@+id/input_amount"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@id/input_date"
            android:layout_toLeftOf="@id/input_date"
            android:hint="@string/amount"
            android:inputType="numberDecimal"/>

    </RelativeLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"/>

</LinearLayout>

Android Studio 预览版:http://i.stack.imgur.com/4bWM6.png

设备上的实际布局:http://i.stack.imgur.com/MJbOX.png

【问题讨论】:

    标签: android android-layout android-linearlayout android-layout-weight


    【解决方案1】:

    发生这种情况是因为您没有向父布局声明标准 WeightSum,因此将 android:weightSum="8" 添加到您的主要 LinearLayout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                  android:weightSum="8">
    
    <!--  your code -->
    
    </LinearLayout>
    

    当您为视图和布局赋予权重时,权重必须为 8116),所以他们父布局的整个weight_sum必须是8

    【讨论】:

    • 谢谢你的建议,虽然结果一模一样。其实我之前也考虑过这条赛道,但this comment 似乎表明 weightSum 没有效果。
    • @Robin 好吧,其实这是我以前不知道的事情
    【解决方案2】:

    ListView 需要 layout_weight 和 input_amount EditText 其余部分是 wrap_content :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/input_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/title"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/input_amount"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:hint="@string/amount"
                android:inputType="numberDecimal"/>
            <Button
                android:id="@+id/input_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/input_date"/>
    
            <Button
                android:id="@+id/input_submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/input_submit"/>
    
        </LinearLayout>
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    </LinearLayout>
    

    【讨论】:

    • 谢谢,这更接近了,但它遇到了与我的布局在使用权重之前相同的问题:input_title 的高度与input_amount 的高度不匹配。关于如何在您提出的解决方案中解决此问题的任何想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多