Android基础——ActionBar
ActionBar指的是app上面那一栏选项
首先要自定义一个资源文件夹menu,在里面写一些菜单选项
<?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" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <!--ActionBar中通过 app:actionViewClass 添加搜索输入框菜单选项--> <item android:id="@+id/search" android:title="search" app:actionViewClass="android.widget.SearchView" app:showAsAction="always" ></item> <!--ActionBar中添加文字菜单选项,溢出菜单选项--> <item android:id="@+id/bell" android:title="消 息" app:showAsAction="ifRoom"></item> <item android:id="@+id/settings" android:title="设 置" app:showAsAction="never" ></item> <!--ActionBar中的添加图片菜单选项--> <item android:id="@+id/about" android:title="关 于" app:showAsAction="always" app:actionLayout="@layout/rabbit_layout"></item> </menu>
java代码里的调用:要先解析菜单布局文件
package com.example.myactionbari; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得ActionBar对象 final ActionBar actionBar = getSupportActionBar(); Button button1 = (Button)findViewById(R.id.button1); Button button2 = (Button)findViewById(R.id.button2); button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { actionBar.hide(); } }); button2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { actionBar.show(); } }); /* * 进入子活动 * */ Button button3 = (Button)findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent( MainActivity.this,StepIntoActivity.class ); Toast.makeText( MainActivity.this,"1",Toast.LENGTH_SHORT ).show(); startActivity(intent); } }); } //解析菜单资源文件 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu,menu); return super.onCreateOptionsMenu(menu); } }
然后是进入一个Activity页面后退出的实现方法
先给子Activity标记父Activity,在注册文件里写
<activity android:name=".StepIntoActivity" android:label="朋友圈"> <!--标记父活动--> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/> </activity>
然后java代码里为ActionBar添加上一个返回箭头
package com.example.myactionbari; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NavUtils; import android.os.Bundle; public class StepIntoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_step_into); //判断父Activity是否为空,不为空设置返回图标 if(NavUtils.getParentActivityName( StepIntoActivity.this) != null){ getSupportActionBar().setDisplayHomeAsUpEnabled(true);//显示向左的导航图标 } } }
相关推荐
西狂杨过 2015-01-14
lizaochengwen 2015-06-30
ohdajing 2017-12-29
xuweinet 2017-04-23
zhouanzhuojinjie 2016-09-21
dangai00 2015-06-30
pengjin 2013-11-11
zhuch 2013-03-16
leafact 2012-06-06
TTHHVV 2013-03-19
StjunF 2014-05-15
叭叭呜 2018-05-03
叭叭呜 2018-05-03
MobileDeviceTalks 2018-05-03
手机APP开发 2017-02-22