[英]How to get pictures to actually respect their bounds in Android?
我正在嘗試進行布局,其中有很多不同的圖片,每張圖片占用的空間相同。 考慮到我希望它們出現在網格中,我使用了TableLayout。
根據這個問題,可以通過調整權重值使每個像元具有相同的寬度和高度。
這就是我在xml中的內容。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="2">
<ImageButton
android:id="@+id/imageButton10"
android:layout_height="match_parent"
android:layout_weight="2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/janice" />
<ImageButton
android:id="@+id/imageButton13"
android:layout_height="match_parent"
android:layout_weight="2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/phoebe" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="2">
<ImageButton
android:id="@+id/imageButton12"
android:layout_height="match_parent"
android:layout_weight="2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/monica" />
<ImageButton
android:id="@+id/imageButton11"
android:layout_height="match_parent"
android:layout_weight="2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/joey" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0sp"
android:layout_weight="2">
<ImageButton
android:id="@+id/imageButton9"
android:layout_height="match_parent"
android:layout_weight="2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/chandler" />
<ImageButton
android:id="@+id/imageButton14"
android:layout_height="match_parent"
android:layout_weight="2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/ross" />
</TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>
我期望的是,由於每個TableRow的權重相同,因此屏幕的高度應平均分為3。然后,由於每個圖像在每個TableRow內的權重均相同,因此每個圖像應占寬度的一半。每個TableRow,都具有良好的網格外觀。 像這樣:
實際發生的情況是這樣的:
我完全意識到這些是不同尺寸的圖像,但是應該可以將它們限制在范圍之內。
是的,還有很多問題,人們會問如何使圖像不占用比預期的更多的空間: ImageView占用太多的垂直空間
但是答案總是固定比例,或者將adjustViewBounds設置為true(我已經完成)。 所以我的問題是
我怎樣才能解決這個問題?
我想出了解決方案。 我認為嘗試使用layout_weight限制每個TableRow的高度首先是錯誤的思維方式。 根據文檔(重點挖掘):
TableLayout的子級不能指定layout_width屬性。 寬度始終為MATCH_PARENT。 但是,layout_height屬性可以由子級定義。 默認值為ViewGroup.LayoutParams.WRAP_CONTENT。 如果子級是TableRow,則高度始終為ViewGroup.LayoutParams.WRAP_CONTENT。
由於無法限制高度,因此圖像可能會占用所需的空間,具體取決於其尺寸。
我們實際上可以使用LinearLayout限制高度。 使用內部帶有三個Horizontal LinearLayouts的Vertical LinearLayout,我們可以為每個圖像提供相同的空間。 每個水平LinearLayout獲取高度的1/3,而每個水平LinearLayout內部的圖片獲取寬度的1/2。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageButton
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
app:srcCompat="@drawable/rachel" />
<ImageButton
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
app:srcCompat="@drawable/phoebe" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<ImageButton
android:id="@+id/imageButton15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
app:srcCompat="@drawable/chandler" />
<ImageButton
android:id="@+id/imageButton17"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
app:srcCompat="@drawable/monica" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<ImageButton
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
app:srcCompat="@drawable/joey" />
<ImageButton
android:id="@+id/imageButton16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
app:srcCompat="@drawable/janice" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.