繁体   English   中英

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

[英]Android layout, resize by percentage and maintain proportion

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

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

您甚至不必担心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 ,但我只是喜欢这种方式。 让我知道这个是否奏效。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM