标签:
搭建环境
<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:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="ifRoom"/>
<!-- 设置, 在溢出菜单中展示 -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// 如果运行的环境 (部署到什么版本的手机 )大于3.0
if (android.os.Build.VERSION.SDK_INT > 11) {
SearchView searchView = (SearchView) menu.findItem(
R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);// 搜索的监听
}
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search) {
Toast.makeText(getApplicationContext(), "搜索", 0).show();
}
return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);
}
// 当搜索提交的时候
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getApplicationContext(), query, 0).show();
return true;
}
// 当搜索的文本发生变化
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 处理动作按钮的点击事件
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void initActionBar() {
super.initActionBar();
ActionBar actionBar = getSupportActionBar();//通过这样或得actionbar
actionBar.setDisplayHomeAsUpEnabled(true);
// 如果你的minSdkVersion属性是11活更高, 应该这么用: // getActionBar().setDisplayHomeAsUpEnabled(true);
}
<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>
<!-- 按钮没有按下的状态 -->
<!-- 没有焦点的状态 -->
<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" />
<!-- 有焦点的状态 (例如D-Pad控制或者鼠标经过) -->
<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" />
<!-- 按钮按下的状态D -->
<!-- 没有焦点的状态 -->
<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" />
<!--有焦点的状态 (例如D-Pad控制或者鼠标经过)-->
<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>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light">
//这里会报错,写着只有11以上能能用,可以先在清单文件中改成11,然后在改成低的就不报错了
<item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">@style/MyActionBarTabs</item>
</style>
<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
parent="@style/Widget.AppCompat.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">@drawable/actionbar_tab_indicator</item>
<!-- Support library compatibility -->
<item name="background">@drawable/actionbar_tab_indicator</item>
</style>
</resources>
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1=actionBar.newTab().setText("标签一").setTabListener(new MyTabListener());//需要这个接口,可以什么也不写
actionBar.addTab(tab1);
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dl"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--如果抽屉没有打开 会显示线性布局 -->
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
<!-- 左面可以滑出来一个抽屉 -->
<!--
<RelativeLayout
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000">
</RelativeLayout> -->
<RelativeLayout
android:layout_gravity="right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00">
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
drawerLayout=(DrawerLayout) findViewById(R.id.dl);
drawerLayout.openDrawer(Gravity.RIGHT);// 打开左面抽屉
ActionBar actionBar = getSupportActionBar();//如果是高版本直接getActionBar
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
1)显示Navigation Drawer的 Activity 对象2) DrawerLayout 对象3)一个用来指示Navigation Drawer的 drawable资源4)一个用来描述打开Navigation Drawer的文本 (用于支持可访问性)。5)一个用来描述关闭Navigation Drawer的文本(用于支持可访问性).drawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout, R.drawable.ic_drawer_am, R.string.open_drawer,
R.string.close_drawer){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Toast.makeText(getApplicationContext(), "抽屉关闭了", 0).show();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Toast.makeText(getApplicationContext(), "抽屉打开了", 0).show();
}
};
mDrawerLayout.setDrawerListener(drawerToggle);
// 让开关和actionbar建立关系
drawerToggle.syncState();
/** 处理actionBar菜单条目的点击事件 */
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search) {
Toast.makeText(getApplicationContext(), "搜索", 0).show();
}
return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);
}
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<android.support.v4.view.PagerTabStrip //这样viepager上面就有了Tab
android:id="@+id/pager_tab_strip“
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#ffffff"
android:textColor="#000"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
private class MainAdpater extends FragmentStatePagerAdapter{
public MainAdpater(FragmentManager fm) {
super(fm);
}
// 每个条目返回的fragment
// 0
@Override
public Fragment getItem(int position) {
if(position==0){
return new HomeFragment();
}else{
return new AppFragment();//现在先这样写,其余的都显示这个fragment
}
}
// 一共有几个条目
@Override
public int getCount() {
return 4;
}
// 返回每个条目的标题
@Override
public CharSequence getPageTitle(int position) {
return "标签"+position;
}
}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4928280.html