【问题标题】:How to create calculator like layout in xml and kotlin combination如何在 xml 和 kotlin 组合中创建类似布局的计算器
【发布时间】:2023-03-26 16:47:02
【问题描述】:

我是 Stack Overflow 的新手。我期待着用 XML 和 Kotlin 组合制作一个计算器。我知道我应该在上面使用 TextView 或 EditText,在下面使用 RecyclerView,但是“如何在 RecyclerView StackOverflow 的一行中添加 3 个按钮”。在所有教程中,我看到它们每行只显示一个元素。但我必须添加 3 个元素。我尝试谷歌搜索“如何在 RecyclerView StackOverflow 的一行中添加 3 个按钮”,但没有得到很好的结果。

我很清楚 RecyclerView 的工作原理,所以我不想要完整的代码。我只需要修改后的哪个部分在 RecyclerView 中获得 3 个按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/calcTv"
        android:layout_marginTop="9dp"
        android:textSize="36dp"
        android:textAlignment="center"
        android:text="5+4"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_below="@+id/calcTv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</RelativeLayout>

除了上面的代码,我还有一个 RecyclerView 适配器和用于布局膨胀的 XML。

如果问题不清楚以及我的提问方式,请提前道歉。

【问题讨论】:

    标签: android xml kotlin android-recyclerview


    【解决方案1】:

    layoutManagerspanCount 属性添加到您的RecyclerView:

    <androidx.recyclerview.widget.RecyclerView
            android:layout_below="@+id/calcTv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:spanCount="3"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>
    

    layoutManager 用于制作列表网格或线性

    spanCount是列数

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 2017-12-20
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多