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

问题描述 投票:0回答:4

我正在使用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

android android-recyclerview gridlayoutmanager
4个回答
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>

OUTPUT

enter image description here

第二种方式

可以将RecyclerViewGridLayoutManager一起使用

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

查看此文章的 RecyclerView with GridLayoutManager

您可以使用RecyclerView的第三种方式

查看本文GridLayoutManager


0
投票

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


0
投票

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

btw,在这种情况下,最好使用RecyclerView和GridLayoutManager。


0
投票

您可以使用TableLayout,并且可以将按钮添加到行和列。选中此GridView

© www.soinside.com 2019 - 2024. All rights reserved.