指令汇C电子市场开发(一) ActionBar的使用

前话:

在学习开发谷歌电子市场的的时候,我换了一款比较高大上的模拟器——genymotion,首先去genymotion的官网注册下载,然后安装。感觉这款模拟器运行挺快的,哈哈,而且可以直接把应用拖进去安装,还可以拖到尺寸,比安卓原生的模拟器好用多啦!原谅我现在才知道大笑


一、ActionBar的使用

我们可以参考安卓官网的文档进行开发,主要分为ActionBar的搜索、按钮返回处理、添加tab标签等操作。

添加ActionBar:

清单文件中:<activity android:theme="@style/Theme.AppCompat.Light" ... >

1、搜索的实现步骤:

在menu中添加

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
</menu>
MainActivity.java中

                 @Override
		public boolean onCreateOptionsMenu(Menu menu) {
			getMenuInflater().inflate(R.menu.activity_main, menu);
			SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
					.getActionView();
			searchView.setOnQueryTextListener(this);//  搜索的监听
			return true;
		}
 
		  // 当搜索提交的时候
		@Override
		public boolean onQueryTextSubmit(String query) {
			Toast.makeText(getApplicationContext(), query, 0).show();
			return true;
		}
		// 当搜索的文本发生变化
		@Override
		public boolean onQueryTextChange(String newText) {
			//Toast.makeText(getApplicationContext(), newText, 0).show();
			return true;
		}

2、返回按钮的处理

1 	
 		
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
2   	
		  
		 <activity  android:name=".DetailActivity"
            android:label="@string/app_detail"
             android:parentActivityName="com.itheima.googleplay.MainActivity"
            >
             <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.itheima.googleplay.MainActivity" />
        </activity>

3、Tab标签

首先在Drawable 目录下 写了一个标签的状态选择器

actionbar_tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/tab_unselected" />
    <item android:state_focused="false" android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@drawable/tab_selected" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/tab_unselected_focused" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@drawable/tab_selected_focused" />

<!-- STATES WHEN BUTTON IS PRESSED -->

    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="true"
        android:drawable="@drawable/tab_selected_pressed" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="true"
          android:drawable="@drawable/tab_selected_pressed" />
</selector>

btn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"  android:enterFadeDuration="200">
    <item  android:state_pressed="true" android:drawable="@drawable/btn_pressed"></item>
    <item  android:drawable="@drawable/btn_normal"></item>

</selector>

在代码里添加标签


ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1=actionBar.newTab().setText("首页").setTabListener(new MyTabListener());
actionBar.addTab(tab1);





posted @ 2015-12-02 20:34  朱培  阅读(202)  评论(0编辑  收藏  举报