Android布局,按百分比调整大小并保持比例

Ric*_*ich 4 layout android

我创建的图像尺寸大于最有可能显示的尺寸.我们说这是200x200.我正在横向模式下测试800x480的设备.我的目标是将此图像调整为当前视图高度的1/4,停靠在右上角,并保持纵横比.这意味着当以800x480观看时,我的图像将显示在120x120的右上角.

我认为这样做的方法是使用一个垂直的LinearLayout,在元素中使用weightSum和layout_weights(如果需要,使用带有layout_weight的空元素进行填充),并在ImageView上使用adjustViewBounds = true,但是我无法获得效果我要去.有任何想法吗?

kco*_*ock 5

您甚至不必担心weightSum或adjustViewBounds,我不这么认为.不过,你应该走在正确的轨道上.试试这个(未经测试的)布局,看看它是否得到你的结果:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <ImageView
        android:id="@+id/qtr_img"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/your_image"
        android:scaleType="fitCenter"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        />
    <View
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这只是在图像下面放置一个空视图,加权占据屏幕的3/4,而ImageView占用剩余的1/4.你也可以使用layout_gravity="right|top",而不是alignParentTopalignParentRight,但我只是喜欢这种方式.让我知道这个是否奏效.