带有加号和减号的可扩展菜单和菜单项
Expandable menu & menu items with plus and minus
图片显示了我想要完成的事情。
在我的 activity 中,我想要一个可扩展的列表视图。单击它时,将显示一个项目列表(列表中的项目将从数据库 table 中填充)。在每个项目旁边,我想显示一个减号、一个加号和一个数字。每次单击加号时,数字都会在显示的数字上加一,或者如果单击减号,则显示的数字会减一。我相信您已经大致了解了项目以及“+”和“-”的工作原理。
我看过这个教程:Android Custom View Tutorial (Part 1) – Combining Existing Views
遗憾的是,本教程仅涵盖加号和减号部分,不包括可扩展视图。
我面临的问题是如何将其印在 Java 中。我读过我应该合并视图,但我不确定如何合并。我在正确的道路上吗?
(总的来说:我有设计但不知道如何编码)
如果我理解正确,您只需要创建可扩展列表视图和适配器(请参阅此处 https://www.codeproject.com/Articles/1151814/Android-ExpandablelistView-Tutorial-with-Android-C),然后在 getChildView()
内膨胀包含您的自定义视图的 R.layout.child_row
(或您可以使用本机 Android 小部件创建布局,如下例所示)。
<!-- child_row.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
tools:text="Menu Item 1" />
<Button
android:id="@+id/bt_less"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="-"
android:textColor="@android:color/holo_red_light" />
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="0" />
<Button
android:id="@+id/bt_more"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="+"
android:textColor="@android:color/holo_green_light" />
</LinearLayout>
图片显示了我想要完成的事情。
在我的 activity 中,我想要一个可扩展的列表视图。单击它时,将显示一个项目列表(列表中的项目将从数据库 table 中填充)。在每个项目旁边,我想显示一个减号、一个加号和一个数字。每次单击加号时,数字都会在显示的数字上加一,或者如果单击减号,则显示的数字会减一。我相信您已经大致了解了项目以及“+”和“-”的工作原理。
我看过这个教程:Android Custom View Tutorial (Part 1) – Combining Existing Views 遗憾的是,本教程仅涵盖加号和减号部分,不包括可扩展视图。
我面临的问题是如何将其印在 Java 中。我读过我应该合并视图,但我不确定如何合并。我在正确的道路上吗? (总的来说:我有设计但不知道如何编码)
如果我理解正确,您只需要创建可扩展列表视图和适配器(请参阅此处 https://www.codeproject.com/Articles/1151814/Android-ExpandablelistView-Tutorial-with-Android-C),然后在 getChildView()
内膨胀包含您的自定义视图的 R.layout.child_row
(或您可以使用本机 Android 小部件创建布局,如下例所示)。
<!-- child_row.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
tools:text="Menu Item 1" />
<Button
android:id="@+id/bt_less"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="-"
android:textColor="@android:color/holo_red_light" />
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="0" />
<Button
android:id="@+id/bt_more"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="+"
android:textColor="@android:color/holo_green_light" />
</LinearLayout>