1、drawerlayout是谷歌官方的侧滑菜单布局,drawerlayout下的第一个布局被视为主布局,第二个被视为左滑菜单,第三个为右滑菜单
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.AppBarOverlay" >
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fl_realcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eee"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<android.support.v4.app.FragmentTabHost
android:id="@+id/tab_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/btn_quickoption_nor"
android:layout_gravity="center"/>
</FrameLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity ="left"
android:background="#eee"
></FrameLayout>
</android.support.v4.widget.DrawerLayout>
2、在Java代码中通过ActionBarDrawerToggle关联toolbar和drawerlayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToolbar =(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout, mToolbar, R.string.app_name, R.string.action_settings);
mActionBarDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
3、在主题样式文件中设置为NoActionBar,防止actionbar和toolbar冲突,style.xml文件如下
<resources>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light">
<!-- 去掉actionbar以防止和toolbar冲突-->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!-- toolbar 上文字的颜色-->
<item name="android:textColorPrimary">@color/white</item>
<!-- android 5.0 Material Design标题栏样式-->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- 设置菜单栏控制图标的颜色为白色 -->
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<!-- 左边的箭头指示-->
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
</resources>
4、fragmentTabhost实现fragment切换
(1) 新建一个Indicator枚举类封装页面底部的tab键
public enum Indicator {
Main(R.drawable.tab_icon_new,"综合", MainFragment.class),
NEWS(R.drawable.tab_icon_tweet,"动弹", NewsFragment.class),
QUICK(R.drawable.tab_icon_new,"综合", MainFragment.class),
FIND(R.drawable.tab_icon_explore,"发现", FindFragment.class),
ME(R.drawable.tab_icon_me,"我的", MeFragment.class);
int resIcon;
String tabName;
Class clz;
Indicator(int resIcon,String tabName,Class clz){
this.resIcon = resIcon;
this.tabName = tabName;
this.clz = clz;
}
public Class getClz() {
return clz;
}
public void setClz(Class clz) {
this.clz = clz;
}
public int getResIcon() {
return resIcon;
}
public void setResIcon(int resIcon) {
this.resIcon = resIcon;
}
public String getTabName() {
return tabName;
}
public void setTabName(String tabName) {
this.tabName = tabName;
}
}
private void initTabHost() {
fragmentTabHost = (FragmentTabHost) findViewById(R.id.tab_host);
fragmentTabHost.setup(MainActivity.this,getSupportFragmentManager(),R.id.fl_realcontent);
Indicator []indicators = Indicator.values();
for(int i=0;i<indicators.length;i++){
TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(indicators[i].getTabName());
View view = getIndicatorView(indicators[i]);
//中间的按钮点击跳转到activity而不是fragment
if(i == 2){
view.setVisibility(View.INVISIBLE);
}
tabSpec.setIndicator(view);
fragmentTabHost.addTab(tabSpec,indicators[i].getClz(),null);
}
//设置fragmenttabhost的间隙为0
fragmentTabHost.getTabWidget().setShowDividers(0);
ImageView ivAdd = (ImageView) findViewById(R.id.iv_add);
ivAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"test",Toast.LENGTH_SHORT).show();
}
});
}