标签:creat 输入框 oncreate lis support enc http tar extend
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);//显示向左的导航图标 } } }
标签:creat 输入框 oncreate lis support enc http tar extend
原文地址:https://www.cnblogs.com/zsben991126/p/12241656.html