我正在構建一個跳棋游戲。棋盤定義為 8 列 8 行的 GridView。每個紅色瓦片是一個 ImageButton,每個黑色瓦片是一個 ImageView(因為紅色瓦片是可播放的,而黑色瓦片不是)。ImageButtons 和 ImageViews 每個都有一個可繪制的物件,可以是紅色格子、黑色格子或無格子。每個可繪制物件為 50dpx50dp。
我試圖讓電路板自行調整大小,使其始終占據可用螢屏的 80% 左右。我嘗試的第一件事是創建可繪制物件的“小”、“大”和“超大”變體,這讓我非常接近,但是“小”螢屏和“大”螢屏之間的區別非常大,這導致在介于“小”和“大”之間的設備上浪費了大量的螢屏空間。
我想根據涉及當前螢屏解析度的計算以編程方式調整可繪制物件的大小。我很高興實作螢屏解析度監聽器,但我不確定如何將監聽器連接到可繪制影像,或者是否有更好的方法來做到這一點。
我做了一些谷歌搜索,看看我能找到什么,我遇到的大部分內容都談到了使用不同的像素密度,但這并不是我真正遇到的問題。我還發現了一堆關于使用相對布局等的東西,但這不適用于棋盤格。
對于如何實作這一點,我將不勝感激。謝謝!
編輯添加
根據@ben-p 的評論,我已經修改了我的代碼,而且我越來越接近了。在我的布局檔案中,我現在有:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<fragment
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在片段布局檔案中,我有:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="8"
android:rowCount="8">
<ImageButton
android:id="@ id/Tile00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#cc0000"
android:contentDescription="@string/game_checkers_red_tile"
android:padding="0dp"
android:src="@drawable/game_checkers_player_none" />
<ImageView
android:background="#000000"
android:contentDescription="@string/game_checkers_black_tile"
android:padding="0dp"
android:src="@drawable/game_checkers_player_none" />
<!-- repeated for a total of 64 tiles -->
</GridLayout>
這會將棋盤限制為螢屏上具有 1:1 縱橫比的正方形,但不會縮放棋盤以顯示所有 8x8 瓷磚。在下面的示例中,您可以看到板被裁剪:

讓 ConstraintLayout 放大/縮小片段內容以使其始終完全適合提供的空間的神奇咒語是什么?
uj5u.com熱心網友回復:
我不得不稍微更改主視圖代碼:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
然后我不得不改變片段布局代碼如下:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="8"
android:rowCount="8">
<ImageButton
android:id="@ id/Tile00"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#cc0000"
android:contentDescription="@string/game_checkers_red_tile"
android:padding="0dp"
android:src="@drawable/game_checkers_player_none" />
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#000000"
android:contentDescription="@string/game_checkers_black_tile"
android:padding="0dp"
android:src="@drawable/game_checkers_player_none" />
<!-- repeated for a total of 64 tiles -->
</GridLayout>
一旦我這樣做了,棋盤現在會擴大以填充可用空間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/454510.html
