【问题标题】:How to create a Linearlayout with specific views as an item (Task) inside another Linearlayout?如何创建具有特定视图的线性布局作为另一个线性布局内的项目(任务)?
【发布时间】:2020-01-03 02:38:54
【问题描述】:

一周前,我开始了我的待办事项列表应用项目,我学到了很多东西。我想知道一个我找不到的具体案例。

案例:我尝试创建一个 Linearlayout 类型的“布局项”作为其中包含特定视图的任务,例如另一个 Linearlayout 中的“任务名称、任务描述、时间、删除按钮”。但事情是这样的,第一个 Linearlayout 的视图在其中都有一个特定的位置。我想保留意见的立场。如何将这种布局创建为项目?

另一个简短的问题:当我创建此布局时,如何以编程方式将其删除?

在这里您可以找到我创建的设计以及“布局项”(任务)应该是什么样子

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:background="#eeeeee"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/fira_sans_extra_condensed_semibold"
                    android:text="Task1"
                    android:textColor="@color/Task_Name"
                    android:textSize="16sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/fira_sans_condensed_light"
                    android:text="Description1 - I want to create a great todo list app :)"
                    android:textSize="14sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <TextView
                android:id="@+id/textView111"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:fontFamily="@font/fira_sans_condensed_light"
                android:gravity="end"
                android:text="9am"
                android:textStyle="bold" />

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="0dp"
                android:layout_height="34dp"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="5dp"
                android:layout_weight="1"
                android:background="@null"
                android:contentDescription="@string/Delete_Icon"
                android:tint="@color/Task_Name"
                app:srcCompat="@android:drawable/ic_menu_delete" />

    </LinearLayout>

【问题讨论】:

  • 但是有什么问题呢?这是一个可以在 RecyclerView 中为每个项目使用的布局。
  • 我想知道的是如何使用我在预览图像中手动创建的视图以编程方式创建线性布局。

标签: java android android-studio android-layout android-intent


【解决方案1】:

Ahmed 是正确的,您将使用回收器并创建一个视图,其中包含:任务名称、任务描述、时间、删除按钮。然后,您可以通过编辑包含您的项目的数组来删除和添加项目。

这是一个简单的回收器示例:Simple Android RecyclerView example

【讨论】:

    【解决方案2】:

    您可以使用此处描述的 RecyclerView:

    https://developer.android.com/guide/topics/ui/layout/recyclerview

    【讨论】:

    • 我阅读了文档并了解了如何使用 recyclerview。非常感谢,艾哈迈德。我只想知道一件事(也许我自己发现了):如何在 recyclerview 中实现日期文本视图并按给定日期值对所有任务进行排序?当我创建一个日期不在任务列表中的新任务时,我想动态创建新的日期文本视图。
    • 查看这个答案,它展示了如何使用 Comparator 的实现并使用它对 RecyclerView 的适配器中的项目列表进行排序:stackoverflow.com/a/48090364/6798074 这个也是:stackoverflow.com/a/50564091/6798074 记得调用数据更改每当一个或多个任务的日期发生更改时,适配器上的通知方法,以便回收器视图将这些更改应用到 UI。
    猜你喜欢
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多