Android:下拉菜单 - 谁有简单的例子?

时间:2014-03-15 13:41:29

标签: android button drop-down-menu

谁有按钮单击下拉菜单的简单示例?

需要制作已安装程序的列表并选择启动。

Menu listMenu = null;
listMenu.add("quasatron"); listMenu.add("magnetron"); listMenu.add("atarrilix");
onCreateOptionsMenu(listMenu);


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.popup_menu, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.food:
            makeToast("food","","","");
            return true;
        case R.id.other:
            makeToast("other","","","");
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

popup_menu的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/food" android:title="Food" />
<item android:id="@+id/other" android:title="Other" />
</menu>

1 个答案:

答案 0 :(得分:3)

此示例创建自己,当您在下拉(微调)列表中选择任何项目时,图像显示在基于选择项目的右侧。所以这个例子对你有帮助。

MainActivity.java

public class MainActivity extends Activity implements OnClickListener, OnItemSelectedListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] technology = {"PHP", "Ruby", "Java", "SQL"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, technology);
        final Spinner spinnertech = (Spinner) findViewById(R.id.spinnertech);
        spinnertech.setAdapter(adapter);

        spinnertech.setOnItemSelectedListener(this);

    // Spinner Start.... 
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        int position = arg0.getSelectedItemPosition();
        ImageView ivtech = (ImageView) findViewById(R.id.imgtech);

        if(position == 0) {
            ivtech.setImageResource(R.drawable.php);
        } else if(position == 1) {
            ivtech.setImageResource(R.drawable.ruby);
        } else if(position == 2) {
            ivtech.setImageResource(R.drawable.java);
        } else if(position == 3) {
            ivtech.setImageResource(R.drawable.sql);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }
    // Spinner End.... 

}

main.xml中

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


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

        <ImageView
            android:id="@+id/imgtech"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:layout_marginRight="10dp" />

        <Spinner
            android:id="@+id/spinnertech"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1" />

    </LinearLayout>

</TableLayout>