2017-08-28 58 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在第二個版本中始終具有假設寬度(使用長文本)。

達到此目的的最佳方法是什麼?

+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

現在看來它在佈局預覽中起作用。但是當我在運行時動態地將圖像加載到imageview中時,尺寸仍然很小 – IARI

+0

您是否在android模擬器上測試過它? 一旦我回到家中,我會與我的一個項目進行比較。我有一個fitXY項目,它的工作很完美 – Used

+0

我已經在運行Android 6.0的Moto G2上測試過它。 – IARI

0
  1. 廣場TextViewImageView不同LinearLayout S和地方兩個第三LinearLayout
  2. 使用match_parent代替wrap_content內爲您TextView寬度
0

您可以ImageView的

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