【问题标题】:App design problems: scroll and amount problems应用程序设计问题:滚动和数量问题
【发布时间】:2014-07-14 19:49:44
【问题描述】:

我是 StackOverflow 的新手,如果这是正确的,我并不完全确定,但我想为我父亲的公司制作一个 android 应用程序,他是 Bed&Breakfirst 的所有者,他想要一个应用程序来跟踪消费情况。我计划了一个产品列表的界面,可以像这样选择所有消费:

http://imgur.com/cwY4YtK

现在的问题是我们需要能够添加和删除产品。否则我会这样做:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget46"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/widget47"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="267dp"
android:layout_y="2dp" />
<Button
android:id="@+id/widget52"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="266dp"
android:layout_y="43dp" />
<TextView
android:id="@+id/widget53"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="9dp"
android:layout_y="10dp" />
<TextView
android:id="@+id/widget54"
android:layout_width="251dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="8dp"
android:layout_y="50dp" />
<Button
android:id="@+id/widget55"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="267dp"
android:layout_y="85dp" />
<Button
android:id="@+id/widget56"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="267dp"
android:layout_y="128dp" />
<TextView
android:id="@+id/widget57"
android:layout_width="259dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="2dp"
android:layout_y="92dp" />
<TextView
android:id="@+id/widget58"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_x="6dp"
android:layout_y="136dp" />
</AbsoluteLayout>

但这只有在您知道自己拥有的产品数量的情况下才能做到。但是我们需要添加和删除产品(编辑不是必需的。)。

此外,如果我们添加许多产品以适应屏幕,我想制作一个滚动选项,可以滚动浏览所有类似的产品。

有没有办法做这两件事或者这是不可能的?

【问题讨论】:

  • 这通常使用ListView 来完成。

标签: java android architecture


【解决方案1】:

A1:

使用 TableLayout(或 GridLayout 等)和页面更改按钮。 (这更容易。)

在此模式中,表格(布局中)具有固定的行数。
将 OnClickListener 设置为下一个/上一个按钮以更新每个单元格(文本视图)文本。
(例如:第1页:显​​示第0到第4行的产品名称,第2页:显示第5到第9行)
单元格文本视图旁边的每个按钮的 OnClickListeners 也需要修改。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/text_01"
                android:layout_width="@dimen/text_view_width"
                android:layout_height="wrap_content"
                android:text="cell 01" />

            <Button
                android:id="@+id/button_01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/text_02"
                android:layout_width="@dimen/text_view_width"
                android:layout_height="wrap_content"
                android:text="cell 02" />

            <Button
                android:id="@+id/button_02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/text_03"
                android:layout_width="@dimen/text_view_width"
                android:layout_height="wrap_content"
                android:text="cell 03" />

            <Button
                android:id="@+id/button_03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/text_04"
                android:layout_width="@dimen/text_view_width"
                android:layout_height="wrap_content"
                android:text="cell 04" />

            <Button
                android:id="@+id/button_04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/text_05"
                android:layout_width="@dimen/text_view_width"
                android:layout_height="wrap_content"
                android:text="cell 05" />

            <Button
                android:id="@+id/button_05"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </TableRow>
    </TableLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Prev" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next" />
    </LinearLayout>

</LinearLayout>

A2:

使用内部包含TableLayout(或GridLayout等)的ScrollView。

在此模式中,必须更改表(布局中)的行数以适合您的数据数。
(在您的代码中创建/删除 TableRows 并从表中添加/删除它们)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/text_01"
                android:layout_width="@dimen/text_view_width"
                android:layout_height="wrap_content"
                android:text="cell 01" />

            <Button
                android:id="@+id/button_01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/text_02"
                android:layout_width="@dimen/text_view_width"
                android:layout_height="wrap_content"
                android:text="cell 02" />

            <Button
                android:id="@+id/button_02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </TableRow>
    </TableLayout>

</ScrollView>

对于 A1 和 A2:

res/values/dimens.xml

<resources>
    <dimen name="text_view_width">250dp</dimen>
</resources>

【讨论】:

    【解决方案2】:

    您可能想使用 android listview 或 gridview,只要适合您。 Here 是一个很好的关于如何使用列表视图的教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      相关资源
      最近更新 更多