Menu菜单和PopupWindow窗口
Menu菜单
系统自带菜单
OptionsMenu这个是系统的菜单,可快速的在应用的右上方使用
系统菜单的布局属性
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--showAsAction属性:always总是 never 从不 ifroom 能显示就显示 默认是nerver-->
<item android:id="@+id/blue" android:title="蓝色" app:showAsAction="never" ></item>
<item android:id="@+id/red" android:title="红色" > </item>
<item android:id="@+id/green" android:title="绿色" ></item>
</menu>
系统菜单的逻辑代码
//重写onCreateOptionsMenu加载资源文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu,menu);
return super.onCreateOptionsMenu(menu);
}
//重写onOptionsItemSelected设置事件监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.blue:
textView.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#A21C31"));
break;
}
return super.onOptionsItemSelected(item);
}
上下文菜单ContextMenu
上下文菜单ContextMenu
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text);
//为控件添加长按属性并将菜单绑定到这个控件上
registerForContextMenu(textView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.options_menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {//设置监听的事件
int id = item.getItemId();
switch (id) {
case R.id.blue:
textView.setTextColor(Color.parseColor("#2239A2"));//做出不同的操作
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#A21C31"));
break;
}
return super.onContextItemSelected(item);
}
}
弹出菜单
弹出菜单的逻辑代码
public class Main2Activity extends AppCompatActivity {
private TextView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
view = (TextView) findViewById(R.id.popup_tv);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu();
}
});
}
//TODO 弹出菜单
public void showPopupMenu(){
//参数一 上下文 参数二 显示在哪一个控件下
PopupMenu popupMenu= new PopupMenu(this,view);
//加载布局,将布局填充进这个菜单中
popupMenu.inflate(R.menu.options_menu);
//事件监听
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.blue:
view.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
view.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
view.setTextColor(Color.parseColor("#A21C31"));
break;
}
return false;
}
});
//显示
popupMenu.show();
}
}
弹出菜单的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:gravity="center">
<TextView
android:id="@+id/popup_tv"
android:text="弹出菜单"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
PopupWindow
Activity的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PopWindowActivity"
android:orientation="vertical"
>
<RelativeLayout
android:background="#C0CCCC"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:gravity="center_vertical"
android:textColor="#585656"
android:textSize="20sp"
android:layout_marginLeft="30dp"
android:text="微信"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/add"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:layout_marginTop="11dp"
android:src="@drawable/star"
android:layout_width="30dp"
android:layout_height="30dp" />
</RelativeLayout>
</LinearLayout>
Activity的java代码
package com.example.myapplication;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.PopupWindow;
public class MainActivity extends AppCompatActivity {
private ImageView add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_add, null);
popupWindow.setContentView(inflate);
popupWindow.setWidth(600);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(add,0,0);
}
});
}
}
弹出窗口的布局
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#fff"
android:textSize="20sp"
android:text="发起群聊"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#fff">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#fff"
android:textSize="20sp"
android:gravity="center"
android:text="发起群聊"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#fff">
</View>
</LinearLayout>