如果我使用.75和.25,为什么布局重量不起作用?

时间:2015-12-14 16:54:09

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

这是我的XML:

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

    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="8 diciembre, 2015"
        android:id="@+id/txtFecha"
        android:textAlignment="textEnd"
        android:layout_margin="5dp"
        android:layout_gravity="right" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:layout_margin="10dp">

        <TextView
            android:layout_width="0dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text"
            android:id="@+id/txtTituloNot"
            android:layout_height="wrap_content"
            android:maxHeight="52dp"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:src="@drawable/feclaabajo"
            android:layout_below="@+id/txtTituloNot"
            android:scaleType="fitXY"
            android:adjustViewBounds="false"
            android:layout_weight="1" />

    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text Medium Text Medium Text Medium Text
        Medium Text Medium Text Medium Text Medium Text Medium Text
        Medium Text Medium Text Medium Text Medium Text Medium Text
        Medium Text Medium Text Medium Text Medium Text Medium Text Medium"
        android:id="@+id/txtExtracto"
        android:layout_margin="5dp"
        android:maxHeight="110dp"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="233dp"
        android:layout_height="170dp"
        android:id="@+id/imageViewNoticia"
        android:src="@drawable/imagenodisponible"
        android:soundEffectsEnabled="false"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitXY"
        android:layout_marginBottom="7dp" />

</LinearLayout>

但现在我只使用以下代码:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:layout_margin="10dp">

    <TextView
        android:layout_width="0dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text"
        android:id="@+id/txtTituloNot"
        android:layout_height="wrap_content"
        android:maxHeight="52dp"
        android:layout_weight="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/feclaabajo"
        android:layout_below="@+id/txtTituloNot"
        android:scaleType="fitXY"
        android:adjustViewBounds="false"
        android:layout_weight="1" />

</LinearLayout>

如您所见,imageview和textview有:

android:layout_weight="1"

但这就是它:

图片视图无法看到,Textview有100%

我设置为Imageview的任何值都不会改变

现在,如果我更改文本的值,但是值介于0和.99之间 (图片视图还有1个重量):

如果我使用.75和.25,这项工作正常,但如果我设置3和1则不起作用 我想能够使用最后一个

1 个答案:

答案 0 :(得分:3)

您已为父LinearLayout定义weightSum为1。

通过这样做,您告诉LinearLayout儿童权重总和的最大权重将为1,但是您将两个孩子的权重设置为1,最多为2。

最简单的解决方案是简单地删除weightSum属性,因为在这种情况下它不是必需的,然后您不必记住保持同步。