带有layout_weight属性的imageView和src无法适合给定的大小

时间:2014-11-06 11:22:54

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

当我尝试在带有layout_weight和图像的LinearLayout中使用ImageView时,ImageView将占据整个空间。

让我说我有这个布局:

<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="10">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/blue"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:background="@color/red"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/green"/>
</LinearLayout>

布局(屏幕上)如下所示:

(发布图片的声誉不够,抱歉)

////////

BLUE LinearLayout(主要布局的1/10)

////////

RED ImageView(主要布局的8/10)

////////

绿色LinearLayout(主要布局的1/10)

////////


但如果我将图像(大一个......)添加到ImageView:

<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="10">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/blue"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:background="@color/red"
        android:src="@drawable/splash"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/green"/>
</LinearLayout>

它看起来像这样:

////////

RED ImageView(主要布局的10/10) - 带图像.....

////////

任何人都知道如何让ImageView的src适合给定的空间(主要LinearLayout的8/10) 感谢。

1 个答案:

答案 0 :(得分:1)

显然这是android studio预览版的问题(在Android API级别19之前)

在部署到设备时,它可以完美运行....

相关问题