通过上下文菜单修改桌面背景色,java代码如下
public class MainActivity1 extends Activity { private static final int GREEN = 1; private static final int BLUE = 2; private static final int RED = 3; private LinearLayout ll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test3); ll = (LinearLayout) findViewById(R.id.ll); registerForContextMenu(ll); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(1, GREEN, 1, "绿色"); menu.add(1, BLUE, 1, "蓝色"); menu.add(1, RED, 1, "红色"); menu.setGroupCheckable(1, true, true);; menu.setHeaderTitle("请选择背景色"); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case GREEN: item.setChecked(true); ll.setBackgroundColor(Color.GREEN); break; case BLUE: item.setChecked(true); ll.setBackgroundColor(Color.BLUE); break; case RED: item.setChecked(true); ll.setBackgroundColor(Color.RED); break; default: break; } return super.onContextItemSelected(item); } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/ll" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击上下文菜单修改桌面背景" /> </LinearLayout>