在Android Studio中将按钮对齐为网格布局

时间:2019-09-26 06:09:30

标签: android android-recyclerview gridlayoutmanager

我正在使用Android Studio制作一个应用,并且希望每行三列并排显示按钮。我设法在第一行实现了这一目标,但第二行却全都搞砸了。这就是我想要的:

enter image description here

这是我尝试过的:

<ScrollView
    android:id="@+id/scrollablContent"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">


    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">


        <Button
            android:id="@+id/englishstatus"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:text="Download Code"
            android:textSize="20sp"
            android:padding="15dp"
            android:textStyle="bold|italic"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="1dp"
            android:background="@drawable/bg" />

        <Button
            android:id="@+id/linestatus"
            android:layout_toRightOf="@id/englishstatus"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:text="Download Code"
            android:layout_centerInParent="true"
            android:background="@drawable/bg" />

        <Button
            android:id="@+id/attitude"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:text="Download Code"
            android:layout_alignParentRight="true"
            android:layout_marginRight="1dp"
            android:background="@drawable/bg" />

我将其作为输出enter image description here

4 个答案:

答案 0 :(得分:2)

有多种方法可以满足您的要求

  

首先使用线性布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher_background" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher_background" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher_background" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher_background" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher_background" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher_background" />

    </LinearLayout>

</LinearLayout>

输出

enter image description here

  

第二种方法

您可以将RecyclerViewGridLayoutManager一起使用

GridLayoutManager  gridLayoutManager = new GridLayoutManager(YourActivity.this, 3);
recyclerView.setLayoutManager(gridLayoutManager);

查看本文的 RecyclerView with GridLayoutManager

  

第三种使用GridView

的方式

查看本文中的Android Grid View

答案 1 :(得分:0)

您必须将Recyclerview与SquareRelativeLayout一起使用,请检查此link

答案 2 :(得分:0)

我认为,您最好提供完整的布局代码。

顺便说一句,在这种情况下最好使用RecyclerView和GridLayoutManager。

答案 3 :(得分:0)

您可以使用TableLayout,还可以向行和列添加按钮。选中此link