如何设置高度等于它的宽度?
How to set the height equal to its width?
我想做一个类似国际象棋的棋盘游戏(8x8 单元格棋盘)。
作为目标,我希望我的应用程序可以在多个智能手机上运行,例如屏幕点(= 720px X 1280px,1024px X 1920px,1024 X 2340px)。
附图是我的游戏板,输入px值使高度等于宽度。在这个结果中,1024px X 1920px 和 1024 X 2340px 都可以。
当然,不适用于 720px X 1280px 智能手机。
能不能给我一些控制 TableLayout 高度等于宽度的技术?
<TableLayout
android:id="@+id/TableLayout"
android:layout_width="1080px"
android:layout_height="1080px"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
enter image description here
对于固定数量的 boxes/grids,我建议使用 GridLayout
或 RecyclerView
和 GridLayoutManager
。以下是 2 X 2 案例的 GridLayout
示例:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"> //specify number of column each row
<TextView
android:layout_columnWeight="1" //【important!! Not "layout_rowWeight"!!】
android:gravity="center" //center content
android:text="Sam"
android:textColor="@color/black"
android:textSize="30sp" />
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:text="Sam"
android:textColor="@color/black"
android:textSize="30sp" />
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:text="Sam"
android:textColor="@color/black"
android:textSize="30sp" />
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:text="Sam"
android:textColor="@color/black"
android:textSize="30sp" />
</GridLayout>
但是,你会注意到它的高度不等于宽度,那是因为我们使用match_parent
来让GridLayout
占满屏幕的宽度,但是我们使用wrap_content
表示它的高度。那么,设置match_parent
为高度?不好。使用硬编码dp
?不好。好吧,事实证明 ConstraintLayout
:
有一个名为 layout_constraintDimensionRatio
的惊人属性
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp" //must be 0dp for either width or height
app:layout_constraintDimensionRatio="1:1" //make the ratio 1:1
android:columnCount="2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
...
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
给你。
我想做一个类似国际象棋的棋盘游戏(8x8 单元格棋盘)。 作为目标,我希望我的应用程序可以在多个智能手机上运行,例如屏幕点(= 720px X 1280px,1024px X 1920px,1024 X 2340px)。
附图是我的游戏板,输入px值使高度等于宽度。在这个结果中,1024px X 1920px 和 1024 X 2340px 都可以。
当然,不适用于 720px X 1280px 智能手机。
能不能给我一些控制 TableLayout 高度等于宽度的技术?
<TableLayout
android:id="@+id/TableLayout"
android:layout_width="1080px"
android:layout_height="1080px"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
enter image description here
对于固定数量的 boxes/grids,我建议使用 GridLayout
或 RecyclerView
和 GridLayoutManager
。以下是 2 X 2 案例的 GridLayout
示例:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"> //specify number of column each row
<TextView
android:layout_columnWeight="1" //【important!! Not "layout_rowWeight"!!】
android:gravity="center" //center content
android:text="Sam"
android:textColor="@color/black"
android:textSize="30sp" />
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:text="Sam"
android:textColor="@color/black"
android:textSize="30sp" />
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:text="Sam"
android:textColor="@color/black"
android:textSize="30sp" />
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:text="Sam"
android:textColor="@color/black"
android:textSize="30sp" />
</GridLayout>
但是,你会注意到它的高度不等于宽度,那是因为我们使用match_parent
来让GridLayout
占满屏幕的宽度,但是我们使用wrap_content
表示它的高度。那么,设置match_parent
为高度?不好。使用硬编码dp
?不好。好吧,事实证明 ConstraintLayout
:
layout_constraintDimensionRatio
的惊人属性
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp" //must be 0dp for either width or height
app:layout_constraintDimensionRatio="1:1" //make the ratio 1:1
android:columnCount="2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
...
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
给你。