Исходники и картинки для статьи Android::Контекстное меню
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:background="@color/red"
android:height="60dp"
android:layout_height="wrap_content"
android:textSize="24dp"
android:layout_width="fill_parent"
android:text="@string/hello"
android:id="@+id/textView"></TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="24dp"
android:height="60dp"
android:layout_marginTop="10px"
android:background="@color/yellow"
android:id="@+id/textView2" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/change_color"
android:title="@string/titleChangeColor" />
<item android:id="@+id/change_textsize"
android:title="@string/titleChangeTextSize" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Нажмите и удерживайте в течение 2 секунд</string>
<string name="app_name">Пример с контекстным меню</string>
<string name="titleChangeColor">Поменять цвет</string>
<string name="titleChangeTextSize">Поменять размер шрифта</string>
<color name="red">#FFFF0000</color>
<color name="yellow">#FFFF00</color>
</resources>
package ru.alexanderklimov.contextmenudemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class ContextMenuDemoActivity extends Activity
{
public static final int IDM_OPEN = 101;
public static final int IDM_SAVE = 102;
public TextView textview2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textView);
textview2 = (TextView)findViewById(R.id.textView2);
registerForContextMenu(textview);
registerForContextMenu(textview2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
// программное добавление меню
menu.add(Menu.NONE, IDM_OPEN, Menu.NONE, "Открыть");
menu.add(Menu.NONE, IDM_SAVE, Menu.NONE, "Сохранить");
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
CharSequence message;
switch (item.getItemId())
{
case IDM_OPEN:
message = "Выбран пункт Открыть";
break;
case IDM_SAVE:
message = "Выбран пункт Сохранить";
break;
case R.id.change_color:
message = "Меняем цвет";
textview2.setBackgroundColor(Color.RED);
break;
case R.id.change_textsize:
message = "Меняем размер шрифта";
textview2.setTextSize(18);
break;
default:
return super.onContextItemSelected(item);
}
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}