当屏幕太小而无法以其自然尺寸包含所有布局元素时(例如,当较小的屏幕进入横向模式时)我注意到它看起来像 Android 首先隐藏 TextViews,然后缩小图像直到他们适合。这是我的印象,也许我并不完全正确。我的问题是:是否可以先调整图像大小?这样,即使图像需要变得非常小,文本仍然可见。
这是众多示例之一。 scaleType 无关紧要。根据我的观察,如果 Android 在屏幕上没有足够的空间并且需要决定是缩小图像还是裁剪/隐藏文本……文本将首先受到伤害。
<?xml version="1.0" encoding="utf-8"?>
<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:src="@drawable/large_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
最佳答案
在这里试试这个:
<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:src="@drawable/large_image"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:scaleType="centerInside" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
这将使您的 textview 变为 wrap_content,即不收缩,而 ImageView 将占用剩余空间,因此如果它开始被压扁,将是 ImageView 被压扁。
如果你担心它变得太大,设置一个 android:maxHeight="200dip"
或类似的东西
关于android - 当没有足够的空间时,如何使图像调整大小而不是文本消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8329233/