【发布时间】:2014-07-30 15:09:03
【问题描述】:
我有一个复杂的 xml 布局,其中一部分是:
...
<FrameLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_weight="1" >
<ImageView
android:id="@+id/flexible_imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/gradient"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingTop="8dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
...
FrameLayout @+id/parent 的高度必须在运行时确定,因为它显示在许多其他视图之上,并且它们必须显示在布局中。这样,FrameLayout 可以完美地填充剩余空间(使用 height="0dp" 和 weight="1" 属性)。
ImageView @+id/flexible_imageview 从网络接收图像,并且始终以正确的纵横比显示。这部分也已经ok了。这个View是最大的,应该决定FrameLayout @+id/parent的大小。
问题是当图像被绘制时,FrameLayout @+id/parent 的宽度没有被调整为换行,而是应该是ImageView @+id/flexible_imageview。
感谢任何帮助。
-- 更新--
我已经更新了布局,以澄清一些缺失的部分,并添加所有这些部分背后的原因。
我想要的是有一个尺寸未知的图像 (ImageView @+id/flexible_imageview),在它上面有一个渐变和一些文本。我无法将FrameLayout @+id/parent 尺寸设置为wrap_content,因为必须显示此图像之后的更多视图。如果布局中没有足够的空间,图像 (ImageView @+id/flexible_imageview) 会缩小,直到它完全适合布局,但它应该保持其纵横比,以及其顶部的渐变/文本。
@drawable/gradient 只是:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270"
android:endColor="#aa000000"
android:startColor="#00000000" />
</shape>
-- 更新 2--
我在下面添加了两张图片来演示正在发生的事情以及应该发生的事情:
错误:
正确:
【问题讨论】:
-
adjustViewBounds 不会影响图像视图的 match_parent 尺寸。当您手动降低高度时,使用默认 scaleType (FIT_CENTER) 绘制时图像正在调整大小,但视图仍然是相同的宽度
-
很抱歉,我不清楚您所说的“在它之上”是什么意思。您的意思是“在顶部分层”(如 z 顺序)还是“朝向屏幕顶部”(如 y 坐标)?
-
@FunkTheMonk 的adjustViewBounds 是用来保持图像的纵横比的,不是吗?不过,我想在这种情况下没有必要。
-
@MikeT 对此我很抱歉;英语不是我的主要语言。我的意思是在顶部分层,就像在 z 轴上一样。 :)
标签: android view android-framelayout viewgroup