0
我在LinearLayout中有一個TableLayout,它是一個4x4網格。我希望每個單元格都是一個正方形,並且我希望大小根據屏幕大小(或分配給TableLayout的大小)進行縮放。我總是希望屏幕上顯示所有12個方格。實質上,我希望每個ImageView的layout_height
和layout_wight
值爲父級值的1/4。現在我已經將它們設置爲100dp
。我需要使用layout_weight
值做些什麼嗎?如何根據屏幕大小調整表格佈局中的元素大小?
編輯:好了,使用layout_weight="1"
我所有的ImageView
對象我能得到寬度都成爲可用空間的1/4,但現在他們已經0的高度。我怎麼能得到的高度也是layout_weight
的函數?
這是我的XML:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.package.name.MainActivity">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 1st row -->
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorAccent"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorPrimary"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorAccent"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorPrimary"/>
</TableRow>
<!-- 2nd row -->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorPrimary"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorAccent"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorPrimary"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorAccent"/>
</TableRow>
<!-- 3rd row -->
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorAccent"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorPrimary"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorAccent"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorPrimary"/>
</TableRow>
<!-- 4th row -->
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorPrimary"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorAccent"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorPrimary"/>
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@color/colorAccent"/>
</TableRow>
</TableLayout>
</LinearLayout>
它看起來像這樣,與最右邊的列沒有完全顯示。我知道這是因爲我把它們都設置爲100dp,但只給目前我在這裏的想法:
您正在使用圖像視圖。你有沒有圖像放入圖像視圖,或者你只是用它們來獲取顏色到正方形? – Cheticamp
我將最終獲得圖像,顏色現在只是一個佔位符。我有點廢棄這個想法,而是使用'GridLayout'來代替。 – intA
我想說如果你的圖像有1x1的比例,你可以在ImageViews上指定adjustViewBounds =「true」,單元格將是方形的。 – Cheticamp