首页 > 解决方案 > android:背景没有反映在布局权重的视图中

问题描述

我有一个简单LinearLayoutImageView视图和普通视图,布局权重分别为 0.55 和 0.45。

除非我给weightsum我的LinearLayout.

拆分后LinearLayout,我尝试为我的视图提供背景颜色,但它没有反映。

下面是我的代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    >
    <ImageView
        android:layout_weight=".55"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="5dip"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher_background"
        android:contentDescription="TODO" />

    <View
        android:layout_weight=".45dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:visibility="visible"
        android:background="@color/colorPrimary"
        ></View>

</LinearLayout>

我不明白应该改变什么,我可以正确分割视图并设置背景颜色。提前致谢!

标签: androidandroid-linearlayoutandroid-layout-weightandroid-background

解决方案


替换"android:layout_weight=".45dp""android:layout_weight=".45"

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

<ImageView
    android:layout_weight=".55"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="5dip"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher_background"
    android:contentDescription="TODO" />

 <View
    android:layout_weight=".45"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:visibility="visible"
    android:background="@color/colorPrimary"/>


</LinearLayout>

推荐阅读