menu菜单
布局
res —>menu文件夹—>xml(Menu resource file)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/blue" android:title="蓝色"></item>
<item android:id="@+id/red" android:title="红色"></item>
<item android:id="@+id/green" android:title="绿色"></item>
</menu>
1.系统菜单 OptionsMenu
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.txt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.blue:
textView.setTextColor(Color.parseColor("#0000ff"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#ff0000"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#00ff00"));
break;
}
return super.onOptionsItemSelected(item);
}
}
2.上下文菜单 ContextMenu
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.txt);
//为控件设置长按属性并将菜单添加到控件上
registerForContextMenu(textView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.my_menu,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.blue:
textView.setTextColor(Color.parseColor("#0000ff"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#ff0000"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#00ff00"));
break;
}
return super.onContextItemSelected(item);
}
}
3.弹出菜单
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.txt);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu();
}
});
}
public void showPopupMenu(){
//第一个参数:上下文,第二个参数:控件(该控件下面谈出)
PopupMenu popupMenu = new PopupMenu(this,textView);
popupMenu.inflate(R.menu.my_menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.blue:
textView.setTextColor(Color.parseColor("#0000ff"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#ff0000"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#00ff00"));
break;
}
return true;
}
});
popupMenu.show();
}
}
PopupWindow
1.在指定控件下
public void showPopupWindow(){
PopupWindow popupWindow = new PopupWindow(this);
popupWindow.setWidth(200);//设置宽
popupWindow.setHeight(300);//设置高
View view = View.inflate(this,R.layout.layout,null);
textView1 = view.findViewById(R.id.search);
textView2 = view.findViewById(R.id.lock);
textView3 = view.findViewById(R.id.photo);
popupWindow.setContentView(view);//设置布局
popupWindow.setOutsideTouchable(true);
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=0.5f;//透明度
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes1 = getWindow().getAttributes();
attributes1.alpha=1f;//消失是取消透明
getWindow().setAttributes(attributes1);
}
});
//点击事件
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, textView1.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, textView2.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, textView3.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
//设置动画
popupWindow.setAnimationStyle(R.style.my_anim1);
//显示且绑定控件
popupWindow.showAsDropDown(imageView);
}
2.沿手机边缘弹出
public void showBottomPopupWindow(){
PopupWindow popupWindow = new PopupWindow(this);
popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(350);
View view = View.inflate(this,R.layout.layout,null);
textView1 = view.findViewById(R.id.search);
textView2 = view.findViewById(R.id.lock);
textView3 = view.findViewById(R.id.photo);
final View view1 = View.inflate(this,R.layout.activity_main,null);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.parseColor("#ff0000"));
}
});
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.parseColor("#00ff00"));
}
});
textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.parseColor("#0000ff"));
}
});
popupWindow.setContentView(view);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
popupWindow.setAnimationStyle(R.style.my_anim2);
//底部弹出
popupWindow.showAtLocation(view1,Gravity.BOTTOM,0,0);
}
简易动画
res—>anim 文件夹—>xml
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000">
<translate
android:fromYDelta="100%"
android:toYDelta="0%">
</translate>
</set>
values中style.xml
<style name="my_anim1">
<item name="android:windowEnterAnimation">@anim/anim_in</item>
<item name="android:windowExitAnimation">@anim/anim_out</item>
</style>
3.自定义
自定义类继承PopupWindow
重写构造 在构造中设置属性
popupWindow.setOutsideTouchable(true)不生效问题
设置一个空背景:
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));