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

时间:2010-11-17 20:08:00

标签: android layout

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

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

2 个答案:

答案 0 :(得分: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>

这只是在图像下方放置一个空视图,加权占据屏幕的3/4,而ImageView占用剩余的1/4。您也可以使用layout_gravity="right|top"代替alignParentTopalignParentRight,但我更喜欢这种方式。如果有效,请告诉我。

答案 1 :(得分:0)

现在就是如果您将创建一个表单并且它支持应用程序到具有与模拟器不同的分辨率(标签未对齐)的电话的方式。 是否可以标准化分辨率,使用“%”作为测量单位而不是px,......?

相关问题