【问题标题】:How can I programm a layout like this?如何编写这样的布局?
【发布时间】:2017-07-29 16:39:27
【问题描述】:

我想创建一个如下所示的布局: https://gyazo.com/2a08bcd731fb1cfeb07e72f75bc05e7e 我应该为此使用什么,它适合所有设备?

屏幕是我的,但问题是它不适合所有屏幕尺寸。 在某些设备上,它看起来像这样: https://gyazo.com/84d878061234c25ae872d4ed2491f2f4

这是我用来编写此布局的代码:

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

如何编写适合各种屏幕尺寸的这样的布局? 重要的是 ScrollView

【问题讨论】:

  • 使用自定义适配器搜索 GridAdapter 以获取 Gride viewRecyclerView
  • 是否可以不在活动中编写任何代码来做到这一点?我只想要按钮的排列方式
  • 我不确定你的意思,但它可以是一个使用 GridAdapter 就像你用 ListView 做的一样,也可以通过使用 RecyclerView AFAIK 来实现!

标签: android xml button gridview layout


【解决方案1】:

想到的第一个想法是使用带有GridLayoutManager的RecyclerView

GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2, LinearLayoutManager.VERTICAL, false);
yourRecyclerView.setLayoutManager(gridLayoutManager);

这将为您提供一个垂直向下的列表,其中包含 2 列。如果您不介意,您可能还想查看 CardView,但有时我喜欢将它们一起使用。

为 RecyclerView 编写适配器后,您可以在 Activity 中设置 GridLayoutManager 和 YourAdapter,这几乎是您在 Activity 中真正需要的唯一东西:初始化适配器,设置布局管理器和适配器你的recyclerview,完成了。你需要自己编写适配器,但是有很多教程你可以谷歌。

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

【讨论】:

  • 是否可以不在活动中编写任何代码来做到这一点?我只想要按钮的那种排列。稍后我将使用 onClick 方法来使用按钮。所以我只想要按钮的排列。有没有更简单的方法?
  • 我真的不知道。但是使用 RecyclerView,您可以编写少于 10 行代码来启动它们。大部分代码将在 Adapter 和 ViewHolder 中(您也可以在其中添加 onClickListener)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
相关资源
最近更新 更多