如何在运行时切换视图?

时间:2012-04-05 16:38:08

标签: android user-interface view android-activity runtime

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

// 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转换,非常简单。

我希望这仍有可能以另一种身份出现,有人可以请我朝正确的方向努力吗?

3 个答案:

答案 0 :(得分:1)

查看ViewSwitcher:请参阅http://developer.android.com/reference/android/widget/ViewSwitcher.html

那,或者将您的活动布局基于包含supplier_invoices.xml和customer_invoices.xml的framelayout。那么你本土的hide-n-show将是g2g。那么,您可能还需要更改ID。

答案 1 :(得分:0)

您可以将视图包含在两个LinearLayouts中,一个用于R.layout.customer_invoices,另一个用于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);
}

我认为你有我的想法。

答案 2 :(得分:0)

这样做

main.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);
        }

    }