Gridlayout - 元素的大小超过扩展过去的元素
Gridlayout - sizing of element over extending past elements
Intro/Issue
我正在使用一个名为 AIDE 的移动应用程序(它是 App UI Designer 程序)在开始编程之前为我正在制作的移动应用程序创建布局。布局近乎完美。我希望 playerTwo 部分位于屏幕顶部,上下颠倒以便两个玩家都能使用它。然后我会在中间添加一个分隔线,playerOne 部分将在底部,中间是空白 space/divider。问题是,当分隔线的高度设置为 Wrap Content 时,它会向下过度延伸出屏幕,将 playerOne 部分推离屏幕。
我需要什么
我需要分隔线不断扩展高度,直到 playerOne 部分位于屏幕底部而 playerTwo 部分位于屏幕顶部。
我试过的
- 更改每个元素的行和列跨度
- 在各种元素上将布局引力更改为居中、填充、垂直填充等
- 修改分割线的行列
- 修改分隔线的重力、边距和填充
- 更改高度以匹配父项
截图
固定高度的布局(10 dp
)
包含环绕内容的布局
XML
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
columnCount="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
rowCount="1"
android:columnCount="3"
android:layout_gravity="right"
android:orientation="horizontal"
android:rowCount="8">
<!-- Start of Player Two Section -->
<!-- Current LP -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="8000 LP"
android:minWidth="100dp"
android:gravity="center"
android:rotation="180"
android:id="@+id/playerTwo_LP"
android:layout_column="1"
android:layout_row="0"
android:layout_rowSpan="2"
android:layout_gravity="fill"/>
<!-- Toolkit -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="TOOLS"
android:rotation="180"
android:id="@+id/playerTwo_toolKit"
android:layout_row="1"
android:layout_column="0"/>
<!-- Cards Tool -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CARDS"
android:layout_row="1"
android:layout_column="2"
android:rotation="180"
android:id="@+id/playerTwo_CardLibrary"/>
<!-- Add LP -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ADD LP"
android:layout_gravity="fill"
android:rotation="180"
android:id="@+id/playerTwo_addLP"
android:layout_row="0"
android:layout_column="0"/>
<!-- Lose LP -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="LOSE LP"
android:layout_column="2"
android:rotation="180"
android:id="@+id/playerTwo_loseLP"
android:layout_row="0"/>
<!-- Player Two Name -->
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ems="10"
android:layout_row="2"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="Seto Kaiba"
android:gravity="center"
android:id="@+id/playerTwo_name"
android:textStyle="italic"
android:rotation="180"/>
<!-- End of Player Two Section -->
<View
android:background="?android:attr/dividerVertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_row="3"
android:layout_rowSpan="1"
android:layout_gravity="center|fill"/>
<!-- Start of Player One Section -->
<!-- Player One Name -->
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ems="10"
android:layout_row="5"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="Yugi Muto"
android:gravity="center"
android:id="@+id/playerOne_name"
android:textStyle="italic"
android:rotation="0"/>
<!-- Current LP -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="8000 LP"
android:minWidth="100dp"
android:gravity="center"
android:rotation="0"
android:id="@+id/playerOne_LP"
android:layout_column="1"
android:layout_row="6"
android:layout_rowSpan="2"
android:layout_gravity="fill"/>
<!-- Toolkit -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="TOOLS"
android:rotation="0"
android:id="@+id/playerOne_toolKit"
android:layout_row="6"
android:layout_column="0"
android:layout_gravity="center"/>
<!-- Cards Tool -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CARDS"
android:layout_row="6"
android:layout_column="2"
android:rotation="0"
android:id="@+id/playerOne_CardLibrary"/>
<!-- Add LP -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ADD LP"
android:layout_gravity="fill"
android:id="@+id/playerOne_addLP"
android:layout_row="7"
android:layout_column="0"/>
<!-- Lose LP -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="LOSE LP"
android:layout_column="2"
android:id="@+id/playerOne_loseLP"
android:layout_row="7"/>
<!-- End of Player One Section -->
</GridLayout>
从根本上说,我认为 GridLayout
是您父级布局的错误选择。您可以通过查看布局来判断它真的 不是网格。当然,您可以使用 span 让它为您工作,但您最好选择一个您不必为之奋斗的父布局。
我推荐ConstraintLayout
。它几乎是为这样的布局量身定做的。你可以告诉它好东西,比如 "put this view in the top-right corner" 和 "put this other view in the bottom left corner"。或者 "stretch this view to fill all the space between these two other views".
如果您重新编写布局以使用 ConstraintLayout
:
,这就是您的布局
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!-- player two section -->
<Button
android:id="@+id/playerTwo_addLP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD LP"
android:rotation="180"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<Button
android:id="@+id/playerTwo_toolKit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOOLS"
android:rotation="180"
app:layout_constraintTop_toBottomOf="@+id/playerTwo_addLP"
app:layout_constraintLeft_toLeftOf="parent"/>
<Button
android:id="@+id/playerTwo_loseLP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOSE LP"
android:rotation="180"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/playerTwo_CardLibrary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CARDS"
android:rotation="180"
app:layout_constraintTop_toBottomOf="@+id/playerTwo_loseLP"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/playerTwo_LP"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="8000 LP"
android:textAppearance="?android:attr/textAppearanceLarge"
android:rotation="180"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/playerTwo_addLP"
app:layout_constraintRight_toLeftOf="@+id/playerTwo_loseLP"
app:layout_constraintBottom_toBottomOf="@+id/playerTwo_toolKit"/>
<EditText
android:id="@+id/playerTwo_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="italic"
android:text="Seto Kaiba"
android:rotation="180"
app:layout_constraintTop_toBottomOf="@+id/playerTwo_toolKit"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<!-- player one section -->
<Button
android:id="@+id/playerOne_addLP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD LP"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<Button
android:id="@+id/playerOne_toolKit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOOLS"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@+id/playerOne_addLP"/>
<Button
android:id="@+id/playerOne_loseLP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOSE LP"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<Button
android:id="@+id/playerOne_CardLibrary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CARDS"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/playerOne_loseLP"/>
<TextView
android:id="@+id/playerOne_LP"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="8000 LP"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintTop_toTopOf="@+id/playerOne_toolKit"
app:layout_constraintLeft_toRightOf="@+id/playerOne_toolKit"
app:layout_constraintRight_toLeftOf="@+id/playerOne_CardLibrary"
app:layout_constraintBottom_toBottomOf="parent"/>
<EditText
android:id="@+id/playerOne_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="italic"
android:text="Yugi Muto"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/playerOne_toolKit"/>
<!-- divider -->
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="?android:attr/dividerVertical"
app:layout_constraintTop_toBottomOf="@+id/playerTwo_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/playerOne_name"/>
</android.support.constraint.ConstraintLayout>
Intro/Issue
我正在使用一个名为 AIDE 的移动应用程序(它是 App UI Designer 程序)在开始编程之前为我正在制作的移动应用程序创建布局。布局近乎完美。我希望 playerTwo 部分位于屏幕顶部,上下颠倒以便两个玩家都能使用它。然后我会在中间添加一个分隔线,playerOne 部分将在底部,中间是空白 space/divider。问题是,当分隔线的高度设置为 Wrap Content 时,它会向下过度延伸出屏幕,将 playerOne 部分推离屏幕。
我需要什么
我需要分隔线不断扩展高度,直到 playerOne 部分位于屏幕底部而 playerTwo 部分位于屏幕顶部。
我试过的
- 更改每个元素的行和列跨度
- 在各种元素上将布局引力更改为居中、填充、垂直填充等
- 修改分割线的行列
- 修改分隔线的重力、边距和填充
- 更改高度以匹配父项
截图
固定高度的布局(10 dp
)
包含环绕内容的布局
XML
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
columnCount="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
rowCount="1"
android:columnCount="3"
android:layout_gravity="right"
android:orientation="horizontal"
android:rowCount="8">
<!-- Start of Player Two Section -->
<!-- Current LP -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="8000 LP"
android:minWidth="100dp"
android:gravity="center"
android:rotation="180"
android:id="@+id/playerTwo_LP"
android:layout_column="1"
android:layout_row="0"
android:layout_rowSpan="2"
android:layout_gravity="fill"/>
<!-- Toolkit -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="TOOLS"
android:rotation="180"
android:id="@+id/playerTwo_toolKit"
android:layout_row="1"
android:layout_column="0"/>
<!-- Cards Tool -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CARDS"
android:layout_row="1"
android:layout_column="2"
android:rotation="180"
android:id="@+id/playerTwo_CardLibrary"/>
<!-- Add LP -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ADD LP"
android:layout_gravity="fill"
android:rotation="180"
android:id="@+id/playerTwo_addLP"
android:layout_row="0"
android:layout_column="0"/>
<!-- Lose LP -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="LOSE LP"
android:layout_column="2"
android:rotation="180"
android:id="@+id/playerTwo_loseLP"
android:layout_row="0"/>
<!-- Player Two Name -->
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ems="10"
android:layout_row="2"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="Seto Kaiba"
android:gravity="center"
android:id="@+id/playerTwo_name"
android:textStyle="italic"
android:rotation="180"/>
<!-- End of Player Two Section -->
<View
android:background="?android:attr/dividerVertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_row="3"
android:layout_rowSpan="1"
android:layout_gravity="center|fill"/>
<!-- Start of Player One Section -->
<!-- Player One Name -->
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ems="10"
android:layout_row="5"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="Yugi Muto"
android:gravity="center"
android:id="@+id/playerOne_name"
android:textStyle="italic"
android:rotation="0"/>
<!-- Current LP -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="8000 LP"
android:minWidth="100dp"
android:gravity="center"
android:rotation="0"
android:id="@+id/playerOne_LP"
android:layout_column="1"
android:layout_row="6"
android:layout_rowSpan="2"
android:layout_gravity="fill"/>
<!-- Toolkit -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="TOOLS"
android:rotation="0"
android:id="@+id/playerOne_toolKit"
android:layout_row="6"
android:layout_column="0"
android:layout_gravity="center"/>
<!-- Cards Tool -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="CARDS"
android:layout_row="6"
android:layout_column="2"
android:rotation="0"
android:id="@+id/playerOne_CardLibrary"/>
<!-- Add LP -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ADD LP"
android:layout_gravity="fill"
android:id="@+id/playerOne_addLP"
android:layout_row="7"
android:layout_column="0"/>
<!-- Lose LP -->
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="LOSE LP"
android:layout_column="2"
android:id="@+id/playerOne_loseLP"
android:layout_row="7"/>
<!-- End of Player One Section -->
</GridLayout>
从根本上说,我认为 GridLayout
是您父级布局的错误选择。您可以通过查看布局来判断它真的 不是网格。当然,您可以使用 span 让它为您工作,但您最好选择一个您不必为之奋斗的父布局。
我推荐ConstraintLayout
。它几乎是为这样的布局量身定做的。你可以告诉它好东西,比如 "put this view in the top-right corner" 和 "put this other view in the bottom left corner"。或者 "stretch this view to fill all the space between these two other views".
如果您重新编写布局以使用 ConstraintLayout
:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!-- player two section -->
<Button
android:id="@+id/playerTwo_addLP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD LP"
android:rotation="180"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<Button
android:id="@+id/playerTwo_toolKit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOOLS"
android:rotation="180"
app:layout_constraintTop_toBottomOf="@+id/playerTwo_addLP"
app:layout_constraintLeft_toLeftOf="parent"/>
<Button
android:id="@+id/playerTwo_loseLP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOSE LP"
android:rotation="180"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/playerTwo_CardLibrary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CARDS"
android:rotation="180"
app:layout_constraintTop_toBottomOf="@+id/playerTwo_loseLP"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/playerTwo_LP"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="8000 LP"
android:textAppearance="?android:attr/textAppearanceLarge"
android:rotation="180"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/playerTwo_addLP"
app:layout_constraintRight_toLeftOf="@+id/playerTwo_loseLP"
app:layout_constraintBottom_toBottomOf="@+id/playerTwo_toolKit"/>
<EditText
android:id="@+id/playerTwo_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="italic"
android:text="Seto Kaiba"
android:rotation="180"
app:layout_constraintTop_toBottomOf="@+id/playerTwo_toolKit"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<!-- player one section -->
<Button
android:id="@+id/playerOne_addLP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD LP"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<Button
android:id="@+id/playerOne_toolKit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOOLS"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@+id/playerOne_addLP"/>
<Button
android:id="@+id/playerOne_loseLP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOSE LP"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<Button
android:id="@+id/playerOne_CardLibrary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CARDS"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/playerOne_loseLP"/>
<TextView
android:id="@+id/playerOne_LP"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="8000 LP"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintTop_toTopOf="@+id/playerOne_toolKit"
app:layout_constraintLeft_toRightOf="@+id/playerOne_toolKit"
app:layout_constraintRight_toLeftOf="@+id/playerOne_CardLibrary"
app:layout_constraintBottom_toBottomOf="parent"/>
<EditText
android:id="@+id/playerOne_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="italic"
android:text="Yugi Muto"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/playerOne_toolKit"/>
<!-- divider -->
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="?android:attr/dividerVertical"
app:layout_constraintTop_toBottomOf="@+id/playerTwo_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/playerOne_name"/>
</android.support.constraint.ConstraintLayout>