Android GridLayout中心水平布局
我正在尝试创建一个GridLayout,其中有两列将居中 我的实际设计是:Android GridLayout中心水平布局,android,android-gridlayout,Android,Android Gridlayout,我正在尝试创建一个GridLayout,其中有两列将居中 我的实际设计是: <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:rowCount="4"
custom:columnCount="2"
android:orientation="horizontal">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:gravity="top|left"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:gravity="top|left"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
它看起来像:
我想把这些按钮放在中间,并且它们之间的间距要完美
可能吗
--编辑:
我还尝试将其放入LinearLayout,但没有结果:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<GridLayout xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:rowCount="4"
custom:columnCount="2"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
</LinearLayout>
你就快到了。我认为这会起作用:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:rowCount="4"
custom:columnCount="2"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
</FrameLayout>
来自:
GridLayout的多余空间分布基于优先级而不是权重
(……)
若要拉伸柱,请确保柱内的所有组件都定义了重力
因此,显然您需要将
layout\u gravity
设置为android:layout\u gravity=“top | center”
(我没有测试过这一点,但从文档中可以看出,它应该沿着这些线。)使用layou width=“wrap\u content”使网格水平环绕其内容
并将其设置为layout\u gravity
至center
:
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
// ......
>
为计算器布局复制此示例=)
下面的代码解决了我的问题
<RelativeLayout 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">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:columnCount="3"
android:rowCount="4"
android:useDefaultMargins="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</GridLayout>
</RelativeLayout>
如果使用约束布局,请在网格布局上使用包裹内容,并在其中创建项目。它将以屏幕为中心
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:columnCount="2"
android:useDefaultMargins="true"
/>
为什么不在线性布局中使用GridLayout
和属性android:gravity=“center”
?!尝试过,没有结果。post edit中的详细信息当然不会更改,因为您必须在GridLayout
中使用属性android:layout\u width=“wrap\u content”
。谢谢!项目之间的间距如何?请看:不,这不会使其水平居中。这将使网格表面内的单元格居中。类似于:| uuuucc |这个解决方案非常适合我是的,在这种情况下框架布局比线性布局工作得更好。这应该是可以接受的答案。因为它工作得非常好
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:columnCount="2"
android:useDefaultMargins="true"
/>