Android:在网格布局中滚动?
在我的Android:在网格布局中滚动?,android,android-layout,android-gridlayout,Android,Android Layout,Android Gridlayout,在我的gridlayout中,我试图添加滚动视图,我还希望我的imagebuttons位于屏幕中心的垂直和水平方向。我已经尝试了android:layou gravity=“center”和android:gravity=“center” 但在水平方向上,我的图像按钮显示在左侧 以下是我的网格布局: <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.c
gridlayout
中,我试图添加滚动视图,我还希望我的imagebuttons
位于屏幕中心的垂直和水平方向。我已经尝试了android:layou gravity=“center”
和android:gravity=“center”
但在水平方向上,我的图像按钮显示在左侧
以下是我的网格布局:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:columnCount="2"
android:rowCount="3"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:layout_marginStart="55dp"
android:layout_marginEnd="25dp"
android:layout_marginBottom="20dp"
tools:context=".Menu">
<ImageButton
android:id="@+id/attendance"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@mipmap/attendance"
android:contentDescription="@string/desc0"
android:layout_row="0"
android:layout_column="0"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:paddingLeft="40dp"
android:paddingRight="40dp" />
<ImageButton
android:id="@+id/homework"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@mipmap/homework"
android:contentDescription="@string/desc1"
android:layout_row="0"
android:layout_column="1"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"/>
<ImageButton
android:id="@+id/notice"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@mipmap/notice"
android:contentDescription="@string/desc2"
android:layout_row="1"
android:layout_column="0"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"/>
<ImageButton
android:id="@+id/student_marks"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@mipmap/student_marks"
android:contentDescription="@string/desc3"
android:layout_row="1"
android:layout_column="1"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"/>
<ImageButton
android:id="@+id/myclass"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@mipmap/myclass"
android:contentDescription="@string/desc5"
android:layout_row="2"
android:layout_column="0"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:paddingLeft="40dp"
android:paddingRight="40dp" />
<ImageButton
android:id="@+id/logout"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@mipmap/logout"
android:contentDescription="@string/desc4"
android:layout_row="2"
android:layout_column="1"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:paddingLeft="40dp"
android:paddingRight="40dp" />
</GridLayout>
水平视图,其中我的“五”和“六”图像按钮未显示,因为我没有滚动视图,图像按钮在此方向左对齐,我希望它们位于中间。
我的垂直视图在中间
您可以将所有布局包装在“滚动视图”中以允许滚动,并将其包装在“相对视图”中以允许按钮居中 大概是这样的:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:columnCount="2"
android:rowCount="3">
... (here the buttons)
</GridLayout>
</RelativeLayout>
</ScrollView>
... (这里是按钮)
您希望如何使用它们?如果你在scrollview中添加android:layout_gravity=“center”已经完成了这两个方向的技巧,请发布一张图片。