如何在Android SDK中创建Stack Panel菜单?

时间:2012-03-27 08:02:34

标签: java android android-layout menu stackpanel

我想为我的小部件配置创建一个类似GWT Stack Panel

的菜单

在android中创建类似类型的菜单是否可行?欢迎任何帮助或建议。

1 个答案:

答案 0 :(得分:4)

使用此布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:text="Bt1"
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></Button>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/linearLayout1">
        <ImageView
            android:layout_width="wrap_content"
            android:id="@+id/imageView1"
            android:layout_height="wrap_content"
            android:src="@drawable/icon"></ImageView>
    </LinearLayout>
    <Button
        android:text="Bt2"
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></Button>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/linearLayout2">
        <ImageView
            android:layout_width="wrap_content"
            android:id="@+id/imageView2"
            android:layout_height="wrap_content"
            android:src="@drawable/icon"></ImageView>
    </LinearLayout>
    <Button
        android:text="Bt3"
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></Button>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/linearLayout3">
        <ImageView
            android:layout_width="wrap_content"
            android:id="@+id/imageView3"
            android:layout_height="wrap_content"
            android:src="@drawable/icon"></ImageView>
    </LinearLayout>
</LinearLayout>

并使用此代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    findViewById(R.id.linearLayout1).setVisibility(View.GONE);
    findViewById(R.id.linearLayout2).setVisibility(View.GONE);
    findViewById(R.id.linearLayout3).setVisibility(View.GONE);

    Button bt1=(Button) findViewById(R.id.button1);
    Button bt2=(Button) findViewById(R.id.button2);
    Button bt3=(Button) findViewById(R.id.button3);
    bt1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            findViewById(R.id.linearLayout1).setVisibility(View.VISIBLE);
            findViewById(R.id.linearLayout2).setVisibility(View.GONE);
            findViewById(R.id.linearLayout3).setVisibility(View.GONE);
        }
    });
    bt2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            findViewById(R.id.linearLayout1).setVisibility(View.GONE);
            findViewById(R.id.linearLayout2).setVisibility(View.VISIBLE);
            findViewById(R.id.linearLayout3).setVisibility(View.GONE);
        }
    });
    bt3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            findViewById(R.id.linearLayout1).setVisibility(View.GONE);
            findViewById(R.id.linearLayout2).setVisibility(View.GONE);
            findViewById(R.id.linearLayout3).setVisibility(View.VISIBLE);
        }
    });
}

您可以在linearlayout1,linearlayout2和linearlayout3中使用您的布局,用于Android的构建堆栈面板。