android - 当没有足够的空间时,如何使图像调整大小而不是文本消失?

标签 android android-layout

当屏幕太小而无法以其自然尺寸包含所有布局元素时(例如,当较小的屏幕进入横向模式时)我注意到它看起来像 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/

相关文章:

java - 这个 "NoSuchMethod"异常中缺少哪个方法?

Android 文件上传使用 HTTP PUT

android - 在 Android 中编译库时 TabLayout 崩溃

android - 想在单个 TextView 中有多种文本显示样式

android - 性能最好的 Android ViewGroup 用于独立定位的 child ? - 相对布局与框架布局

android - ADB 备份/恢复多用户应用程序?

棉花糖或更高版本的窗口类型 2010 的 Android 权限被拒绝

Android 布局 - 何时使用应用程序 : vs android:?

java - ImageView 未以线性布局显示(android)

Android 4.0 和 HTML5 历史 API