Android:滚动网格布局?

时间:2016-02-17 11:50:51

标签: android android-layout android-gridlayout

在我的gridlayout中,我正在尝试添加滚动视图,并且我还希望我的imagebuttons位于屏幕中心的垂直方向上。我试过了android:layout_gravity="center"android:gravity="center" 但在水平方向,我的图像按钮出现在左侧。

这是我的Gridlayout:

<?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>

水平视图我的&#34;五&#34;和&#34;六&#34;图像按钮没有显示因为我没有滚动视图,图像按钮在这个方向左对齐,我希望它们在中心。 view in centre

我的垂直视图位于中心 horizontal view

1 个答案:

答案 0 :(得分:3)

您可以将所有布局包装在&#34; ScrollView&#34;允许滚动和内部的&#34; RelativeLayout&#34;允许按钮居中。

这样的事情:

<?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>