Android 我如何编程这样的布局?

Android 我如何编程这样的布局?,android,xml,button,gridview,layout,Android,Xml,Button,Gridview,Layout,我想创建一个如下所示的布局: 我应该用什么来实现这一点,它适合每一个设备 屏幕是我的,但问题是它不适合所有屏幕大小。 在某些设备上,它如下所示: 以下是我用来标记此布局的代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android

我想创建一个如下所示的布局: 我应该用什么来实现这一点,它适合每一个设备

屏幕是我的,但问题是它不适合所有屏幕大小。 在某些设备上,它如下所示:

以下是我用来标记此布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/button1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Button"/>

            <Button
                android:id="@+id/Button2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_column="1"
                android:layout_row="0"
                android:text="Button"/>

            <Button
                android:id="@+id/Button3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_column="0"
                android:layout_row="1"
                android:text="Button"/>

            <Button
                android:id="@+id/Button4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_column="1"
                android:layout_row="1"
                android:text="Button"/>

              //and 200 buttons more

    </GridLayout>

</ScrollView>

//还有200多个按钮
我如何编程一个这样的布局,适合每一个屏幕大小?
重要的是ScrollView

首先想到的是将RecyclerView与GridLayoutManager结合使用

GridLayoutManager GridLayoutManager=newgridlayoutmanager(context,2,LinearLayoutManager.VERTICAL,false);
setLayoutManager(gridLayoutManager);
这将为您提供一个垂直向下的列表,其中包含两列。如果您不介意的话,您可能也想查看CardView,但有时我喜欢将它们一起使用

一旦为RecyclerView编写了适配器,您就可以在活动中设置GridLayoutManager和适配器,这几乎是您在活动中实际需要做的唯一事情:初始化适配器,设置RecyclerView的布局管理器和适配器,然后完成。你需要自己编写适配器,但是有很多教程可以在谷歌上搜索


当我开始学习RecyclerView时,我想我已经遵循了这一点。它有时是以前的,所以我不确定它是否仍然正确,但您可以先检查它。

搜索
GridAdapter
以查看
grid视图
RecyclerView
使用自定义适配器是否可以不在活动中写入任何代码就完成此操作?我只想要按钮的排列我不确定你的意思,但它可以是一个使用
GridAdapter
的按钮,就像你使用
ListView
一样,也可以通过使用
RecyclerView
AFAIK来实现!不在Activitys中编写任何代码就可以做到这一点吗?我只想要那种钮扣的排列。稍后,我将使用onClick方法来使用按钮。所以我只想要按钮的排列。有没有更简单的方法?我真的不知道。但使用RecyclerView,您只需编写可能少于10行的代码即可在活动中启动它们。大部分代码将在适配器和ViewHolder中(您也可以在其中添加onClickListener)。