我想要一个 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 中有短文本和长文本:
我需要 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>
结果:
关于android - 使 Imageview 假定其父级允许的最大宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45924178/