android - 使 Imageview 假定其父级允许的最大宽度

标签 android android-layout

我想要一个 Imageview 展开并假定最大宽度尊重父 View 组边距。但是,当动态隐藏 ImageView 时,父级的宽度必须表现得像 wrap_content。 考虑以下布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Short Text"/>
</LinearLayout>

下图显示了布局的外观, TextView 中有短文本和长文本:

Layout with short text Layout with long text

我需要 ImageView 始终具有第二个版本中的假定宽度(带有长文本)。

实现此目标的最佳方法是什么?

最佳答案

试试这个:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/strasse_hintergrund"/>

也许会改变:

    android:layout_width="match_parent" 

    android:layout_width="wrap_content" 

编辑:

这是我的代码:

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




    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="19dp"
        android:background="@color/dark"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_weight="0.02">

        <TextView
            android:id="@+id/textView2"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="7pt"
            android:textColor="@color/white"/>

    </android.support.v7.widget.Toolbar>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        />

    <TextView
        android:id="@+id/textView"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="10pt"/>

</LinearLayout>

结果:

enter image description here

关于android - 使 Imageview 假定其父级允许的最大宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45924178/

相关文章:

java - Android 和 Firebase 将 EditText 与实时数据库子值进行比较

android - 水平 RecylerView 多行显示不正确

android - 如果我已经为我的应用程序提供了 Android 许可,我是否应该对我的应用程序进行复制保护?

java - 如何在android中动态创建按钮?

android - XStream 和 Proguard

android - Cordova Phonegap 中 Android WebView 上的 HTML5 相机捕获

android - 错误 : You must supply a resource ID for a TextView

android - 在输入过程中显示字符数

android - 将 ViewPager 与原生 fragment 一起使用

java - onCreateView() 在第二次调用后没有显示从数据库中获取的任何数据