ActionBar
定义:译名为动作栏,它是一种窗口特性,可以定义使用位置,提供给使用所要执行的动作以及导航模式。它可以 自适应不同屏幕。
样式:
2.常用主题:
- @android:style/Theme.Holo.NoActionBar:无动作栏
- @android:style/Theme.Holo.Light:灰色动作栏
- @android:style/Theme.Holo.DarkActionBar:默认主题,为黑色
页面效果
逻辑代码--MainActivity
package com.example.week4_day3_actionbardemo1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements
OnClickListener {
/**
* ScrollView的使用:1 声明ScrollView, 2 在菜单布局中添加包
* android:actionViewClass="android.widget.SearchView"
*/
private Button btn1;
private Button btn2;
private Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
Intent intent=new Intent(MainActivity.this, ActionBar1.class);
startActivity(intent);
break;
case R.id.btn2:
Intent intent2=new Intent(MainActivity.this, ActionBar2.class);
startActivity(intent2);
break;
case R.id.btn3:
Intent intent3=new Intent(MainActivity.this, ActionBar3.class);
startActivity(intent3);
break;
default:
break;
}
}
}
逻辑代码--ActionBar1
package com.example.week4_day3_actionbardemo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.SearchView;
public class ActionBar1 extends Activity{
private SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);// 加载菜单布局
return true;
}
}
逻辑代码--ActionBar2
package com.example.week4_day3_actionbardemo1;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
public class ActionBar2 extends Activity {
// android:uiOptions="splitActionBarWhenNarrow":拆分ActionBar
// 将ActionBar显示在底部
private SearchView searchView;
private Button btn1;
private ActionBar bar;// 声明ActionBar
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbar);
btn1 = (Button) findViewById(R.id.btn1);
tv = (TextView) findViewById(R.id.tv);
bar = getActionBar();// 得到ActionBar对象
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (bar.isShowing()) {
bar.hide();// 如果显示就隐藏
} else {
bar.show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);// 加载菜单布局
MenuItem item = menu.findItem(R.id.action_search);
searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new OnQueryTextListener() {
// 按enter进行搜索
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
tv.setText("=setOnQueryTextListener=>" + query);
return false;
}
// 值改变 就搜索
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
tv.setText("=onQueryTextChange=>" + newText);
return false;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
tv.setText("点击了ActionBar");
break;
case R.id.menu_favorite:
tv.setText("点击了折叠中的ActionBar");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
逻辑代码--ActionBar3
package com.example.week4_day3_actionbardemo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.SearchView;
public class ActionBar3 extends Activity{
private SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);// 加载菜单布局
return true;
}
}
布局文件--main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="无动作栏ActionBar"
/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="灰色动作栏ActionBar"
/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认主题ActionBar"
/>
</LinearLayout>
布局文件--actionbar
<?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" >
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示或隐藏ActionBar"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#00ff00"
android:layout_marginTop="200dp"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
菜单布局文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_search"
android:orderInCategory="1"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always"
android:icon="@drawable/search"
android:title="@string/menu_search"/>
<item
android:id="@+id/menu_save"
android:orderInCategory="2"
android:showAsAction="always"
android:icon="@drawable/save"
android:title="@string/menu_save"/>
<item
android:id="@+id/menu_open"
android:orderInCategory="3"
android:showAsAction="collapseActionView"
android:title="@string/menu_open"/>
<item
android:id="@+id/menu_eoe"
android:orderInCategory="4"
android:showAsAction="collapseActionView"
android:title="@string/menu_eoe"/>
<item
android:id="@+id/menu_favorite"
android:orderInCategory="5"
android:showAsAction="collapseActionView"
android:title="@string/menu_favorite"/>
</menu>
值文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">week4_day3_ActionBarDemo1</string>
<string name="action_settings">Settings</string>
<string name="menu_search">查找</string>
<string name="menu_save">保存</string>
<string name="menu_open">打开</string>
<string name="menu_eoe">视察</string>
<string name="menu_favorite">爱好</string>
<string name="menu_gender">性别</string>
</resources>