我们怎样才能将菜单放到屏幕上?

时间:2011-05-19 13:57:07

标签: android menu

我想在屏幕顶部并排放置5张图像。 当我点击一个图像时,我想水平显示其他图像的列表 谁能建议我?

1 个答案:

答案 0 :(得分:0)

例如,您可以在布局XML文件中创建2个嵌套的水平LinearLayouts,其中第二个为空,但具有ID,如下所示:

<?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">
<LinearLayout android:id="@+id/linearLayout1"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:orientation="horizontal">
    <ImageButton android:layout_height="wrap_content" android:id="@+id/imageButton1" android:src="@drawable/icon" android:layout_width="wrap_content" android:padding="0px"></ImageButton>
    <ImageButton android:layout_height="wrap_content" android:id="@+id/imageButton2" android:src="@drawable/icon" android:layout_width="wrap_content" android:padding="0px"></ImageButton>
    <ImageButton android:layout_height="wrap_content" android:id="@+id/imageButton3" android:src="@drawable/icon" android:layout_width="wrap_content" android:padding="0px"></ImageButton>
    <ImageButton android:layout_height="wrap_content" android:id="@+id/imageButton4" android:src="@drawable/icon" android:layout_width="wrap_content" android:padding="0px"></ImageButton>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"></LinearLayout>

然后在Java代码中,您可以通过第一行按钮onClickListeners动态添加第二行图像,如下所示(示例仅将第1行图像添加到第二行):

firstButtonInFirstLine = (ImageButton) findViewById(R.id.imageButton1);
    firstButtonInFirstLine.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LinearLayout secondLinearLayout = (LinearLayout) findViewById(R.id.linearLayout2);

            ImageButton btn = new ImageButton(MainAct.this);
            btn.setImageResource(R.drawable.misc);
            secondLinearLayout.addView(btn);
        }
    });

您可以使用可点击的ImageView元素实现相同的功能。