我想讓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的:
我需要的ImageView在第二個版本中始終具有假設寬度(使用長文本)。
達到此目的的最佳方法是什麼?
這聽起來像你問的不能接受的行爲設置此
android:scaleType="fitXY"
屬性。您希望父級寬度由其子級定義,但您希望子級的寬度也由父級限制。 –不,我不希望父母完全確定寬度 - 父母在默認情況下有效地限制了WRAP_CONTENT的最大寬度,這是我希望從父母處獲得的所有控件。 – IARI