2017-08-28 9 views
2

Imageviewを展開し、親ビューグループの余白に最も大きな幅があるとします。ただし、ImageViewが動的に非表示になっている場合、親の幅はwrap_contentのようになります。 次のレイアウトを考えてみましょう: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> 

以下の画像は、レイアウトがどのように見えるかを示し、両方短いとのTextViewで長いテキストで:

Layout with short text Layout with long text

私はImageViewのを必要とします2番目のバージョン(長いテキスト)のように、いつも想定の幅になります。

これを達成する最も良い方法は何ですか?

+0

ため、このandroid:scaleType="fitXY"プロパティを設定することができます。親の幅をその子によって定義したいが、子の幅も親によって制限されることを望む。 –

+0

いいえ、私は親が幅を完全に決めることを望んでいません - 親は実際にはデフォルトの場合でもWRAP_CONTENTで子の最大幅を制限します。 – IARI

答えて

1

このお試しください:

<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

+0

レイアウトプレビューでは動作するようです。しかし、実行時にイメージビューにイメージを動的にロードすると、サイズはまだ小さいです – IARI

+0

アンドロイドエミュレータでテストしましたか? 家に帰ると、自分のプロジェクトの1つと比較します。私はfitXYと1つのプロジェクトがあり、それは完全に動作します – Used

+0

私はアンドロイド6.0を実行してMoto G2でそれをテストしました。 – IARI

0
  1. 場所TextViewと異なるLinearLayout s内のImageViewと第三LinearLayout OR
  2. 使用match_parentの代わりに、あなたのTextView
ため wrap_contentの内側の両方を配置
0

あなたは不可能行動を求めているようですねImageViewの

<ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/your_image"/> 
関連する問題