NoticationManager类是一个通知管理器类, 这个对象是由系统维护的服务,是以单例模式的方式获得,所以
一般并不直接实例化这个对象。在Activity中,可以使用Activity.getSystemService(String)方法获取
NotificationManager对象,Activity.getSystemService(String)方法 可以通过Android系统级服务的句柄,返回 对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_ SERVICE即可。
使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都
能正常工作。Android8.0新增 了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示
通知渠道:Android 8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。
IMPORTANCE_NONE:关闭通知
IMPORTANCE_MIN:开启通知,不会弹出,但没有提示音,状态栏中无显示
IMPORTANCE_Low:开启通知,不会弹出,不发出提示音,状态栏中显示
IMPORTANCE_DEFAULT:开启通知,不会弹出,发出提示音,状态栏中显示
IMPORTANCE_HIGH:开启通知,会弹出,发出提示音,状态栏中显示
public class WangJiActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wangji_activity);
Toolbar t = findViewById(R.id.t);
//给标题行的图标设置点击事件
t.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
public class MainActivity extends Activity {
int year, month, day;
DatePicker datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker = findViewById(R.id.dp1);
//获取一个日历对象 对它进行实例化
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
//初始化日期选择器
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
MainActivity.this.year = i;
MainActivity.this.month = i1;
MainActivity.this.day = i2;
show(i, i1, i2);
}
});
}
private void show(int year, int month, int day) {
String str = year + "年" + (month + 1) + "月" + day + "日";
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
}
public class MainActivity extends AppCompatActivity {
TimePicker timePicker ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = findViewById(R.id.tp1);
//默认为12小时进制 如果想设置为24小时 则需把下面修改为true
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int i, int i1) {
String str = i + "时" + i1 + "分";
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
android:format:设置显示的时间格式显示格式,默认是"MM:SS"或"H:MM:SS",以%s来格式化
public class MainActivity extends AppCompatActivity {
Chronometer ch;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ch = findViewById(R.id.c1);
//设置起始时间
ch.setBase(SystemClock.elapsedRealtime());//SystemClock.elapsedRealtime()获取当前时间
ch.setFormat("%s");
// ch.setCountDown(true);//设置为倒计时
//开启计时器
ch.start();
//计时监听器
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
//判断计时时间是否到达60秒
if (SystemClock.elapsedRealtime()-ch.getBase()>=60000){
ch.stop();//停止计时
}
}
});
}
}
android:thumb:替换掉拖动按钮的图片
android:max:最大拖动长度比
android:progress:当前长度比
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBar seekBar = findViewById(R.id.sb);
//设置拖动监听器
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override//进度改变的时候执行的方法 i表示当前进度
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Toast.makeText(MainActivity.this, "进度改变:"+i, Toast.LENGTH_SHORT).show();
}
@Override//开始触摸的时候执行的方法
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "开始触摸", Toast.LENGTH_SHORT).show();
}
@Override//停止触摸的时候执行的方法
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "停止触摸", Toast.LENGTH_SHORT).show();
}
});
}
}
android:entries:设置列表项的内容
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner s = findViewById(R.id.s);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String r = s.getItemAtPosition(i).toString();
Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
**可见:**运行状态(正常运行时)、暂停状态(弹出退出询问对话框)、**不可见:**停止状态(确认退出时)、销毁状态(在手机应用程序信息中,点击强制停止)
创建java类继承Activity
重写onCreate()回调方法
设置要显示的视图
package com.example.activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
public class Activity extends android.app.Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Demo3">
<activity
android:name=".Activity2"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<-- 此处为配置内容 --!>
<activity
android:name=".Activity"
android:label="详细" />
<-- 到这结束--!>
application>
manifest>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//创建Intent对象
Intent intent = new Intent(MainActivity.this,Activity.class);
//启动Activity
startActivity(intent);
}
});
}
}
public class Activity extends android.app.Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.finish);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//关闭当前的Activity
finish();
}
});
}
}
什么是Bundle?
以键值对的形式进行保存,通过key拿到value值
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = findViewById(R.id.bt1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editText1 = findViewById(R.id.et1);
String s1 = editText1.getText().toString();
EditText editText2 = findViewById(R.id.et2);
String s2 = editText2.getText().toString();
EditText editText3 = findViewById(R.id.et3);
String s3 = editText3.getText().toString();
EditText editText4 = findViewById(R.id.et4);
String s4 = editText4.getText().toString();
EditText editText5 = findViewById(R.id.et5);
String s5 = editText5.getText().toString();
if (!"".equals(s1) && !"".equals(s2) && !"".equals(s3) && !"".equals(s4) && !"".equals(s5)) {
Intent intent = new Intent(MainActivity.this,BActivity.class);
//创建Bundle保存信息
Bundle bundle = new Bundle();
bundle.putCharSequence("s1",s1);
bundle.putCharSequence("s2",s2);
bundle.putCharSequence("s3",s3);
bundle.putCharSequence("s4",s4);
bundle.putCharSequence("s5",s5);
//将Bundle保存到intent里
intent.putExtras(bundle);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "请将收货地址填写完整", Toast.LENGTH_SHORT).show();
}
}
});
}
}
public class BActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bactivity);
//先获取传过来的intent对象
Intent intent = getIntent();
//取出intent里的Bundle
Bundle bundle = intent.getExtras();
String s1 = bundle.getString("s1");
String s2 = bundle.getString("s2");
String s3 = bundle.getString("s3");
String s4 = bundle.getString("s4");
String s5 = bundle.getString("s5");
TextView textView = findViewById(R.id.text1);
textView.setText(s1 + " " + s2 + " " + s3 + " " + s4 + ":" + s5);
}
}
public class MainActivity extends AppCompatActivity {
//ActivityResultLauncher需要放在外面提前注册
ActivityResultLauncher<Intent> objectActivityResultLauncher = objectActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == 0x11 && result.getData()!=null){
Bundle bundle = result.getData().getExtras();
int imageId = bundle.getInt("imageId");
ImageView imageView = findViewById(R.id.image1);
imageView.setImageResource(imageId);
}
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.bt1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, HeadActivity.class);
//启动activity
objectActivityResultLauncher.launch(intent);
}
});
}
}
public class HeadActivity extends AppCompatActivity {
public int[] imageId = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_head);
Intent intent = getIntent();
Bundle bundle = new Bundle();
bundle.putInt("imageId", imageId[1]);
intent.putExtras(bundle);
setResult(0x11, intent);
//关闭当前Activity
finish();
}
}
public class ListFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//加载一个布局文件
View view = inflater.inflate(R.layout.fragment_list,container,false);
return view;
}
}
<fragment
android:id="@+id/list"
android:name="com.example.fragment2.ListFragment"//要引用的fragment的java文件
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建fragment实列
DetailFragment detailFragment = new DetailFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
//第一个参数是容器可以填写FrameLayout(帧布局管理器)的id,第二参数是fragment
ft.add(android.R.id.content,detailFragment);
//提交
ft.commit();
}
}
Component name: 组件名称,用来设置Intent组件名称
//ComponentName c = new ComponentName("应用的包名","类名(包含包名)"
ComponentName c= new ComponentName("com.example.intent","com.example.intent.DetailActivity");
Intent setComponent(c)
Action:动作
Data:数据
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView dianhua = findViewById(R.id.dianhua);
TextView duanxin = findViewById(R.id.duanxin);
dianhua.setOnClickListener(o);
duanxin.setOnClickListener(o);
}
View.OnClickListener o = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
TextView t = (TextView) view;
//点击电话
if (t.getId() == R.id.dianhua){
//设置动作
i.setAction(i.ACTION_DIAL);//标识用于调用拨号面板
i.setData(Uri.parse("tel:15546290218"));//设置intent的data属性
}else {//点击短信
//设置动作
i.setAction(i.ACTION_SENDTO);//标识发送短信
i.setData(Uri.parse("smsto:15546290218"));
//设置短信默认内容
i.putExtra("sms_body","Hello word");
}
startActivity(i);
}
};
}
给app开启(电话 短信)权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actionordata">
<!--开启打电话的权限-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!--允许发送短信的权限-->
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Demo3">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Category:种类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.b);
//点击按钮返回到手机桌面
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(intent.ACTION_MAIN);
//设置Category的属性
intent.addCategory(intent.CATEGORY_HOME);//桌面
startActivity(intent);
}
});
}
Extras:附加信息
putExtras();//传输Bundle对象
getExtras();//取出Bundle对象
Flags:标志
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,DetailActivity.class);
//让当前activity不在历史栈中保留,用户离开它,这个activity就自动关闭
intent.setFlags(intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
}
Intent的种类
创建Intent对象的语法格式如下:
Intent intent = new Intent(Context packageContext,Class<T>class)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.b);
button.setOnClickListener(new View.OnClickListener() {
//通过隐式intent打开网页
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(intent.ACTION_VIEW);//这个常量标识把数据显示给用户
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
}
}
Intent过滤器
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Demo3" >
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!------------------------新增位置------------------------->
<activity
android:name=".ShowActivity"
android:exported="true">
<!--intent过滤器-->
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<!------------------------新增位置------------------------->
</application>
点击事件
Button b = findViewById(R.id);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//点击触发的方法
}
});
触摸事件
public boolean onTouchEvent(MotionEvent event) {
//触摸触发的方法
return super.onTouchEvent(event);
}
按下物理按钮事件
public boolean onKeyDown(int keyCode, KeyEvent event) {
//按下触发的方法
return super.onKeyDown(keyCode, event);
}
抬起物理按钮事件
public boolean onKeyUp(int keyCode, KeyEvent event) {
//抬起触发的方法
return super.onKeyUp(keyCode, event);
}
长按物理按钮事件
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
//长按物理按钮事件
return super.onKeyLongPress(keyCode, event);
}
长按事件
T.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
//长按执行的方法
return false;
}
});
值改变事件
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//文字值改变时执行
}
@Override
public void afterTextChanged(Editable editable) {
}
});
物理按键的常量
触摸事件监听器
T.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
手势检测
字符串资源
颜色资源
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t = findViewById(R.id.tv);
//此写法需要在build.gradle配置文件中将sdk最小版本调整为23
t.setTextColor(getColor(R.color.bg));
//此写法不需要调整sdk
t.setBackgroundColor(getResources().getColor(R.color.title));
}
}
```
尺寸资源
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.tv);
//设置字体
textView.setTextSize(getResources().getDimension(R.dimen.title));
}
}
布局资源:
在一个布局文件中包含另一个布局文件(xml):
<include layout="@layout/image"></include>
数组资源
定义数组资源文件(xml):
使用数组资源:
1. 在xml文件中引用
2. 在java中引用:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取字符串数组资源
String [] arr = getResources().getStringArray(R.array.list);
}
}
Drawable资源
res/drawable下创建drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--得到或失去焦点的样式-->
<item android:state_focused="true" android:color="#f60"/>
<item android:state_focused="false" android:color="#0a0"/>
</selector>
引用drawable文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/editext_focused"
android:text="嗨害嗨"/>
<EditText
android:id="@+id/e2"
android:textColor="@drawable/editext_focused"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="嗨害嗨2"/>
</LinearLayout>
主题资源
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.bgTheme);//需要放在setContentView()方法之前
setContentView(R.layout.activity_main);
}
}
样式资源
<style name="title">
<item name="android:textSize">30sp</item>
<item name="android:textColor">#06F</item>
</style>
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/title" //在这里引用样式资源
android:hint="请输入密码"/>
<style name="title">
<item name="android:textSize">30sp</item>
<item name="android:textColor">#06F</item>
</style>
<style name="context" parent="title">//子元素
//字体大小18替换了30
<item name="android:textSize">18sp</item>
</style>
菜单资源
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--生成了四个菜单项-->
<item android:id="message" android:title="消息"></item>
<item android:id="homepage" android:title="首页"></item>
<item android:id="help" android:title="帮助"></item>
<item android:id="feedback" android:title="我要反馈"></item>
</menu>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//重写onCreateOptionsMenu方法添加选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = new MenuInflater(MainActivity.this);
menuInflater.inflate(R.menu.menu,menu);//参数一:菜单资源,参数二:上面的menu对象
return super.onCreateOptionsMenu(menu);
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(MainActivity.this, Settings.class);
startActivity(intent);
break;
case R.id.regard:
Intent intent1 = new Intent(MainActivity.this, Regard.class);
startActivity(intent1);
break;
}
return super.onOptionsItemSelected(item);
}
上下文菜单
registerForContextMenu(TextView);
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo){
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu,menu);
}
public boolean onContextItemSelected(MenuIntem item){
//菜单选项被选择时执行的事件
return super onContextItemSelected(item)
}
在AndroidMainifest.xml里设置:
//不显示Action Bar
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
在java中设置ActionBar的显示与隐藏
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取Action Bar
ActionBar actionBar = getSupportActionBar();
Button bt1 = findViewById(R.id.xianshi);
Button bt2 = findViewById(R.id.yincang);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//显示Action Bar
actionBar.show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//隐藏Action Bar
actionBar.hide();
}
});
}
}
创建菜单项
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/serch"
android:icon="@drawable/img"
android:title="serch"
app:showAsAction="always" >item>
<item android:id="@+id/bell"
android:icon="@drawable/img_1"
android:title="消 息"
app:showAsAction="ifRoom"/>
<item android:id="@+id/settings"
android:title="设置"
app:showAsAction="ifRoom"/>
<item android:id="@+id/about"
android:title="关于"
app:showAsAction="never"/>
menu>
解析菜单文件
//解析菜单文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//创建MenuInflater对象
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
创建菜单项
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/serch"
android:title="搜 索"
app:showAsAction="always"/>
<item android:id="@+id/img1"
android:title="通讯录"
app:showAsAction="always"
app:actionLayout="@layout/img_message"/>
menu>
隐藏ActionBar上显示的标题和解析菜单项
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.先隐藏ActionBar上的显示标题
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
//2.解析菜单资源文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
}