将 android 工作室中的 table 设为屏幕高度的一半
Make table in android studio half the height of the screen
基本上,我有一个带有一堆按钮的 table,并且我意识到高度在多种屏幕尺寸上保持固定。是否有一个高度单位是屏幕尺寸的百分比,还是有其他方法?这是 table 的 XML。
所以现在,我希望 table 的高度是它所在屏幕尺寸的一半。在大多数像素设备上,它很好,但对于 tablets 在纵向模式下,它只有屏幕的四分之一。 table 布局 rn 的高度是包装内容,但如果您可以更改它我没问题。
<TableLayout
android:id="@+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input"
app:layout_constraintVertical_bias="1.0">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/clearBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/raleway_medium"
android:onClick="clearBTN"
android:text="@string/clear"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#ff4757" />
<Button
android:id="@+id/paranthesisBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/raleway_medium"
android:onClick="paranthesisBTN"
android:text="@string/parantheses"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff4757" />
<Button
android:id="@+id/exponentBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/raleway_medium"
android:onClick="exponentBTN"
android:text="@string/exponent"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff4757" />
<Button
android:id="@+id/divideBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/raleway_medium"
android:onClick="divideBTN"
android:text="@string/divide"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff4757" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/sevenBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:gravity="center"
android:onClick="sevenBTN"
android:text="@string/seven"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/eightBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="eightBTN"
android:text="@string/eight"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/nineBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="nineBTN"
android:text="@string/nine"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/multiplyBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff9500"
android:fontFamily="@font/nunito_bold"
android:onClick="multiplyBTN"
android:text="@string/multiply"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff9500" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/fourBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="fourBTN"
android:text="@string/four"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/fiveBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="fiveBTN"
android:text="@string/five"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/sixBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="sixBTN"
android:text="@string/six"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/subtractBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/nunito_bold"
android:onClick="subtractBTN"
android:text="@string/subtract"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff9500" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/oneBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="oneBTN"
android:text="@string/one"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/twoBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="twoBTN"
android:text="@string/two"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/threeBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="threeBTN"
android:text="@string/three"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/addBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/nunito_bold"
android:onClick="addBTN"
android:text="@string/add"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff9500" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/plusMinusButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="plusMinusBTN"
android:text="@string/plusMinus"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#505050" />
<Button
android:id="@+id/zeroBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="zeroBTN"
android:text="@string/zero"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#505050" />
<Button
android:id="@+id/pointBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="pointBTN"
android:text="@string/point"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#505050" />
<Button
android:id="@+id/equalsBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher_background"
android:onClick="equalsBTN"
android:text="@string/equals"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff9500" />
</TableRow>
</TableLayout>
提前致谢
有两种常见的获取屏幕百分比(或者实际上是父布局百分比)的策略:
- 使用
LinearLayout
加上layout_weight
- 使用
ConstraintLayout
加上Guideline
线性布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
...>
</TableLayout>
</LinearLayout>
在这里,我们在两个视图上结合了 0dp
的高度和 1
的权重。这将导致 space 在两者之间平分,并且由于第一个视图是 Space
(不可见视图),结果将是 table 占屏幕的一半。
您可以使用任何您想要的重量值。如果你想要 66% 的屏幕,你可以给 Space 权重 1
和 TableLayout 权重 2
;比例是最重要的。
约束布局
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>
<TableLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/guideline"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
...>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这里我们在ConstraintLayout中放了一个Guideline
(一个看不见的“锚点”)。通过将它的百分比设置为 0.5
,我们将它放在屏幕的正中间。之后,我们只需相对于指南定位 TableLayout。
如果您想使 table 变大或变小,您只需移动参考线即可。
基本上,我有一个带有一堆按钮的 table,并且我意识到高度在多种屏幕尺寸上保持固定。是否有一个高度单位是屏幕尺寸的百分比,还是有其他方法?这是 table 的 XML。 所以现在,我希望 table 的高度是它所在屏幕尺寸的一半。在大多数像素设备上,它很好,但对于 tablets 在纵向模式下,它只有屏幕的四分之一。 table 布局 rn 的高度是包装内容,但如果您可以更改它我没问题。
<TableLayout
android:id="@+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input"
app:layout_constraintVertical_bias="1.0">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/clearBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/raleway_medium"
android:onClick="clearBTN"
android:text="@string/clear"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#ff4757" />
<Button
android:id="@+id/paranthesisBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/raleway_medium"
android:onClick="paranthesisBTN"
android:text="@string/parantheses"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff4757" />
<Button
android:id="@+id/exponentBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/raleway_medium"
android:onClick="exponentBTN"
android:text="@string/exponent"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff4757" />
<Button
android:id="@+id/divideBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/raleway_medium"
android:onClick="divideBTN"
android:text="@string/divide"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff4757" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/sevenBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:gravity="center"
android:onClick="sevenBTN"
android:text="@string/seven"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/eightBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="eightBTN"
android:text="@string/eight"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/nineBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="nineBTN"
android:text="@string/nine"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/multiplyBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff9500"
android:fontFamily="@font/nunito_bold"
android:onClick="multiplyBTN"
android:text="@string/multiply"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff9500" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/fourBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="fourBTN"
android:text="@string/four"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/fiveBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="fiveBTN"
android:text="@string/five"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/sixBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="sixBTN"
android:text="@string/six"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/subtractBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/nunito_bold"
android:onClick="subtractBTN"
android:text="@string/subtract"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff9500" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/oneBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="oneBTN"
android:text="@string/one"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/twoBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="twoBTN"
android:text="@string/two"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/threeBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="threeBTN"
android:text="@string/three"
android:textColor="#ffffff"
android:textSize="36sp"
app:backgroundTint="#505050" />
<Button
android:id="@+id/addBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="#ff4757"
android:fontFamily="@font/nunito_bold"
android:onClick="addBTN"
android:text="@string/add"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff9500" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/plusMinusButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="plusMinusBTN"
android:text="@string/plusMinus"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#505050" />
<Button
android:id="@+id/zeroBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="zeroBTN"
android:text="@string/zero"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#505050" />
<Button
android:id="@+id/pointBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:fontFamily="@font/nunito_bold"
android:onClick="pointBTN"
android:text="@string/point"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#505050" />
<Button
android:id="@+id/equalsBTN"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher_background"
android:onClick="equalsBTN"
android:text="@string/equals"
android:textColor="#ffffff"
android:textSize="36sp"
android:textStyle="bold"
app:backgroundTint="#ff9500" />
</TableRow>
</TableLayout>
提前致谢
有两种常见的获取屏幕百分比(或者实际上是父布局百分比)的策略:
- 使用
LinearLayout
加上layout_weight
- 使用
ConstraintLayout
加上Guideline
线性布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
...>
</TableLayout>
</LinearLayout>
在这里,我们在两个视图上结合了 0dp
的高度和 1
的权重。这将导致 space 在两者之间平分,并且由于第一个视图是 Space
(不可见视图),结果将是 table 占屏幕的一半。
您可以使用任何您想要的重量值。如果你想要 66% 的屏幕,你可以给 Space 权重 1
和 TableLayout 权重 2
;比例是最重要的。
约束布局
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>
<TableLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/guideline"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
...>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这里我们在ConstraintLayout中放了一个Guideline
(一个看不见的“锚点”)。通过将它的百分比设置为 0.5
,我们将它放在屏幕的正中间。之后,我们只需相对于指南定位 TableLayout。
如果您想使 table 变大或变小,您只需移动参考线即可。