【问题标题】:adjustViewBounds isn't working in Android layout fileadjustViewBounds 在 Android 布局文件中不起作用
【发布时间】:2012-09-14 05:44:41
【问题描述】:

我已经看过这个帖子:ImageView adjustViewBounds not working,但它不适用于我的情况。

基本上,adjustViewBounds 不起作用。让我们假设以下内容:

square_icon => 1000x1000
rectangle_icon => 400x100
device height => 1000

所以我想要发生的是四个图标的垂直堆叠,尺寸如下:

square_icon = 400x400
rectangle_icon = 400x100
square_icon = 400x400
rectangle_icon = 400x100

但发生的情况是 square_icon 并没有占用它可以占用的所有空间......而 rectangle_icon 更像是 400x200 而不是 400x100?图像没有以不正确的纵横比进行缩放,只是在视图中添加了空白空间(scalingType 确定空间是在顶部、底部还是两者兼而有之)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/square_icon" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/rectangle_icon" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/square_icon" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/rectangle_icon" />

</LinearLayout>

【问题讨论】:

  • 作为后续,如果我从两个 square_icon ImageViews 中取出 android:adjustViewBounds="true" 行,那么 rectangle_icon ImageViews 的高度是正确的......但是,宽度square_icon 变得不正确(它变成一个矩形)
  • 我想我回答了我自己的问题......链接的线程实际上确实适用于这种情况,因为我的图像实际上比我试图放大它们的图像要小!显然你不能在放大时使用adjustViewBounds...
  • 增加了所有图像的大小......但仍然面临同样的问题。我不知所措 - 有什么想法吗?
  • 从第三个 ImageView 中删除 android:adjustViewBounds="true" 现在使其适用于大屏幕和超大屏幕......小屏幕和普通屏幕仍然无法正常工作。一定是bug?它没有逻辑只在某些屏幕上工作?

标签: android layout imageview


【解决方案1】:

我解决了这个问题,对于那些感兴趣的人,通过使用权重明确指定 rectangle_icon 的高度... 为每个 square_icon 分配了 4 的权重,为每个 rectangle_icon 分配了 1 的权重。花了我一辈子,但我终于想通了!

【讨论】:

  • 每个ImageView还需要layout_height=0px,所以整个垂直空间根据指定的layout_weight在它们之间公平分割..
猜你喜欢
  • 2019-02-12
  • 1970-01-01
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多