Android 使Imageview采用其父视图允许的最大宽度

Android 使Imageview采用其父视图允许的最大宽度,android,android-layout,Android,Android Layout,我希望Imageview展开,并假定相对于父视图组边距的最大宽度。但是,当ImageView被动态隐藏时,父对象的宽度必须表现为包裹内容。 考虑以下布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android

我希望Imageview展开,并假定相对于父视图组边距的最大宽度。但是,当ImageView被动态隐藏时,父对象的宽度必须表现为
包裹内容
。 考虑以下布局:

<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>

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

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

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

试试以下方法:

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

编辑:

这是我的密码:

 <?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>

  • TextView
    ImageView
    放在不同的
    LinearLayout
    s中,并将两者都放在第三个
    LinearLayout
    中或
  • 使用
    match_parent
    而不是
    wrap_content
    来查看
    TextView
    width

  • 您可以为Imageview设置此属性

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/your_image"/>
    
    
    
    听起来你在要求不可能的行为。您希望父级的宽度由其子级定义,但您希望子级的宽度也由父级限制。不,我不希望父级完全确定宽度-在默认情况下,父级也使用WRAP_内容有效地约束子级的最大宽度,这就是我想要从父控件获得的所有控件。它现在似乎可以在布局预览中工作。但是,当我在运行时将图像动态加载到imageview中时,大小仍然很小。您是否在android模拟器上对其进行了测试?一旦我回到家,我将与我的一个项目进行比较。我确实有一个fitXY项目,它运行得很好。我已经在运行android 6.0的Moto G2上测试了它。请看下面的代码,它可能会对您有所帮助,因为对我来说,它工作得很好。所以我想,类比应该是id/imageView和id/textView。我的理解是,它之所以有效,是因为您的线性布局中有android:layout\u width=“match\u parent”。但是,如果您在代码中隐藏ImageView,布局将不再收缩以环绕TextView,因此它不能满足我的要求。
     <?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>
    
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/your_image"/>