Android Studio 1-2
- 一、Menu菜单
- 1.系统菜单 OptionsMenu
- 2.上下文菜单 ContextMenu
- 3.弹出菜单
- 二、PopupWindow 窗口
一、Menu菜单
1.系统菜单 OptionsMenu
package com.example.day02ex;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 1, 1, "条目1");
menu.add(1, 3, 3, "条目3");
menu.add(1, 2, 2, "条目2");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.red:
Log.i(TAG, "onOptionsItemSelected: 红色");
break;
case R.id.blue:
Log.i(TAG, "onOptionsItemSelected: 蓝色");
break;
case R.id.yellow:
Log.i(TAG, "onOptionsItemSelected: 黄色");
break;
case 1:
Log.i(TAG, "onOptionsItemSelected: 111");
break;
case 2:
Log.i(TAG, "onOptionsItemSelected: 222");
break;
case 3:
Log.i(TAG, "onOptionsItemSelected: 333");
break;
default:
}
return super.onOptionsItemSelected(item);
}
}
2.上下文菜单 ContextMenu
package com.example.day02ex;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class ConText_Activity extends AppCompatActivity {
private TextView tv2;
private static final String TAG = "ConText_Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context_);
tv2 = (TextView) findViewById(R.id.tv_2);
registerForContextMenu(tv2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(1, 1, 1, "条目1");
menu.add(1, 3, 3, "条目3");
menu.add(1, 2, 2, "条目2");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case 1:
Log.i(TAG, "onOptionsItemSelected: 111");
break;
case 2:
Log.i(TAG, "onOptionsItemSelected: 222");
break;
case 3:
Log.i(TAG, "onOptionsItemSelected: 333");
break;
case R.id.red:
Log.i(TAG, "onOptionsItemSelected: 红色");
break;
case R.id.blue:
Log.i(TAG, "onOptionsItemSelected: 蓝色");
break;
case R.id.yellow:
Log.i(TAG, "onOptionsItemSelected: 黄色");
break;
default:
}
return super.onContextItemSelected(item);
}
}
3.弹出菜单
package com.example.day02ex;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;
public class Pop_Activity extends AppCompatActivity {
private TextView tvPop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_);
tvPop = (TextView) findViewById(R.id.tv_pop);
tvPop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(Pop_Activity.this, tvPop);
;
popupMenu.inflate(R.menu.menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
int itemId = menuItem.getItemId();
switch (itemId) {
case R.id.red:
break;
case R.id.blue:
break;
case R.id.yellow:
break;
default:
}
return false;
}
});
popupMenu.show();
}
});
}
}
二、PopupWindow 窗口
package com.example.day02ex;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
public class POP_WindowActivity extends AppCompatActivity {
private ImageView ivAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_pop__window);
ivAdd = findViewById(R.id.iv_add);
ivAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupWindow = new PopupWindow(POP_WindowActivity.this);
View inflate = LayoutInflater.from(POP_WindowActivity.this).inflate(R.layout.layout_weixin_popupwindow, null);
popupWindow.setContentView(inflate);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(300);
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 0.5f;
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 1f;
getWindow().setAttributes(attributes);
}
});
popupWindow.setAnimationStyle(R.style.pop_window);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(ivAdd);
}
});
}
}