使ImageView适合屏幕高度的一半并填满屏幕宽度

时间:2012-12-10 18:30:58

标签: java android xml image resize

如何:http://i49.tinypic.com/352lxdt.png 我想要的是:http://i45.tinypic.com/bahc1.png

如何通过XML和代码完成此操作。 我正在使用RelativeLayout。 也想要API等级8(来自android 2.3+).. 所以不能使用setWidth(),setHeight() 帮助..?

1 个答案:

答案 0 :(得分:1)

尝试使用具有权重和的LinearLayout包装RelativeLayout,并为RelativeLayout提供所需的权重。例如:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@android:color/black"
            android:scaleType="fitXY" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</LinearLayout>