1

我的会计应用程序中有以下代码:

// switch View to the Customer layout, widget id's are the same on both layouts
private void hideExpenseView() {
    setContentView(R.layout.customer_invoices);
}

// switch View to the Supplier layout
private void hideIncomeView() {
    setContentView(R.layout.supplier_invoices);
}

以上不起作用,因为当您切换 ContentView 时,您会丢失所有变量映射。不幸的是,您必须在 setContentView() 之后映射变量。

如果这可行,这对我的应用程序来说将是一个非常简单的解决方案。看,我已经将两个 xml 布局中的小部件命名为相同的 ID。我没有根据不同的状态隐藏一个 xml 布局的元素,而是将整个视图切换到适当的布局 - 无论是输入客户销售发票还是供应商费用发票。

通过切换视图,我基本上只需要 6 行代码来处理 UI 转换,非常简单。

我希望这仍然可以以另一种身份进行,有人可以将我推向正确的方向吗?

4

3 回答 3

1

查看 ViewSwitcher:见http://developer.android.com/reference/android/widget/ViewSwitcher.html

或者将您的活动布局基于包含供应商发票.xml 和客户发票.xml 的框架布局中。那么你自己的 hide-n-show 将是 g2g。寿,您可能仍需要更改 ID。

于 2012-04-05T16:46:28.910 回答
0

您可以将视图包装成两种LinearLayouts,一种是 for R.layout.customer_invoices,另一种是 for R.layout.supplier_invoices

您需要实现自己的 findViewById。

private static final int LAYOUT_EXPENSE = 1;
private static final int LAYOUT_INCOME = 2;

private int currentLayout = LAYOUT_EXPENSE;

private LinearLayout expenseContainer, incomeContainer;

// switch View to the Customer layout, widget id's are the same on both layouts
private void hideExpenseView() {
    switchLayout(LAYOUT_INCOME);
}

// switch View to the Supplier layout
private void hideIncomeView() {
    switchLayout(LAYOUT_EXPENSE);    
}

private void switchLayout(int layout) {
    currentLayout = layout;
    if (layout == LAYOUT_EXPENSE) { 
        expenseContainer.setVisibility(VISIBLE);
        incomeContainer.setVisibility(GONE);
    } else {
        expenseContainer.setVisibility(GONE);
        incomeContainer.setVisibility(VISIBLE);
    }
}

public View findViewById(int id) {
    if (layout == LAYOUT_EXPENSE) return expenseContainer.findViewById(id);
    else return incomeContainer.findViewById(id);
}

我想你明白了我的想法。

于 2012-04-05T16:50:40.203 回答
0

这样做

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/customer_invoices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- put customer_invoices related tools like TextView, Button, ImageView here -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/supplier_invoices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- put supplier_invoices related tools like TextView, Button, ImageView here -->

    </LinearLayout>

</LinearLayout>

Java代码:

public class TestActivity extends Activity {
    View supplier_invoices,customer_invoices;
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

        supplier_invoices = findViewById(R.id.supplier_invoices);

        customer_invoices = findViewById(R.id.customer_invoices);

        }


     // switch View to the Customer layout, widget id's are the same on both layouts
        private void hideExpenseView() {
            setContentView(R.layout.customer_invoices);
            customer_invoices.setVisibility(View.VISIBLE);
            supplier_invoices.setVisibility(View.GONE);

        }

        // switch View to the Supplier layout
        private void hideIncomeView() {

            supplier_invoices.setVisibility(View.VISIBLE);
            customer_invoices.setVisibility(View.GONE);
        }

    }
于 2012-04-05T17:04:23.137 回答