ActionBar:Toolbar标题栏
介绍:
Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件,以此来取代之前的Actionbar 。我们需要在工程中引入appcompat-v7的兼容包以便向下兼容, 使用android.support.v7.widget.Toolbar进行开发。在设计 Toolbar 的时候,Google也留给了开发者很多可定制修改的余地,这些可定制修改的属性在API文档中都有详细介绍。
常用方法
1.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);去掉标题栏;
2.Toolbar.setLogo(),设置logo图片;
3.Toolbar.setTitle().设置标题;
4.Toolbar.setSubTitle()设置子标题;
5.Toolbar.setTitleTextColor(int color);设置标题文字颜色;
6.Toolbar.setSubtitleTextColor();设置子标题文字颜色;
7.setTitleMargin(int start, int top, int end, int bottom);设置标题margin值; 8.onCreateOptionsMenu,getMenuInflater().inflate(R.menu.menu,menu)
设置菜单在给Toolbar设置为actionbar时使用;
9.Toolbar.setOnMenuItemClickListener();Toolbar绑定menu监听;
10.Toolbar.inflateMenu(R.menu.menu)在Toolbar没有替换actionbar时使用;
11.setSupportActionBar(mToolbar);设置toolbar替换actionbar;
12.getLayoutInflater().inflate(R.layout.view_tv,bar);Toolbar添加自定义view
DrawerLayout:DrawerLayout可以实现侧滑
常用的方法:
DrawerLayout.isDrawerOpen(Gravity.LEFT)是否开启;
DrawerLayout.openDrawer(Gravity.LEFT);开启抽屉
DrawerLayout.closeDrawer(Gravity.RIGHT);关闭抽屉
SlideMenu
属性:
设置模式: setMode(SlidingMenu.LEFT);
设置触摸屏幕的模式:setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//TOUCHMODE_FULLSCREEN全屏;TOUCHMODE_MARGIN边界;TOUCHMODE_NONE不能滑动
设置左侧菜单滑动显示的内容:slidingMenu.setMenu(R.layout.slide_menu);
设置左侧滑动菜单的阴影宽度:slidingMenu.setShadowWidth(300);
设置滑动时的渐变程度:slidingMenu.setFadeDegree(0.5f);范围0.0f-1.0f
设置淡入淡出的效果:slidingMenu.setFadeEnabled(true);
设置左侧滑动菜单的阴影图片(颜色):setShadowDrawable();
设置滑出时主页面显示的剩余宽度:slidingMenu.setBehindOffset(200);
注意:
(1)attachToActivity():将slidemenu和Activity绑定
(2)setMenu():设置菜单显示的内容
(3) Button bt = slidingMenu.getMenu().findViewById(R.id.bt1);为侧滑出来的菜单设置事件监听
ToolBar和DrawerLayout案例
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#999">
</android.support.v7.widget.Toolbar>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开抽屉"
android:onClick="open"/>
<android.support.v4.widget.DrawerLayout
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="right">
<Button
android:id="@+id/ctBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="抽屉中的按钮"
android:onClick="close"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Activity
public class MainActivity extends AppCompatActivity {
private Toolbar toolBar;
private DrawerLayout draw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//不用自带的bar
setContentView(R.layout.activity_main);
toolBar = (Toolbar) findViewById(R.id.toolBar);
draw = (DrawerLayout) findViewById(R.id.draw);
toolBar.setLogo(R.mipmap.ic_launcher_round);
toolBar.setNavigationIcon(R.mipmap.ic_launcher_round);
toolBar.setTitle("工具条");
toolBar.setSubtitle("1705A工具条");
View inflate = LayoutInflater.from(this).inflate(R.layout.custom_bar_layout, null);
toolBar.addView(inflate);//添加自定义部分
setSupportActionBar(toolBar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tool_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.menu1) {
Toast.makeText(this, "我是第一个", Toast.LENGTH_SHORT).show();
}else if (item.getItemId()==R.id.menu2){
Toast.makeText(this, "我是第二个", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
public void open(View view) {
draw.openDrawer(Gravity.RIGHT);
}
public void close(View view) {
draw.closeDrawer(Gravity.RIGHT);
}
}
SlideMenu实现抽屉
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main3Activity"
android:orientation="vertical">
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/re_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="left">
<Button
android:id="@+id/button1_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_margin="20dp"
android:text="第一个"/>
<Button
android:id="@+id/button2_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_margin="20dp"
android:text="第二个"/>
<Button
android:id="@+id/button3_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_margin="20dp"
android:text="第三个"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Activity
private RelativeLayout reLayout;
private Button button1Id;
private Button button2Id;
private Button button3Id;
private FirstFragment one = new FirstFragment();
private SecoundFragment two=new SecoundFragment();
private ThirdFragment three=new ThirdFragment();
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
getSupportActionBar().hide();
reLayout = (RelativeLayout) findViewById(R.id.re_layout);
button1Id = (Button) findViewById(R.id.button1_id);
button2Id = (Button) findViewById(R.id.button2_id);
button3Id = (Button) findViewById(R.id.button3_id);
fragmentManager = getSupportFragmentManager();
button1Id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.re_layout,one);
fragmentTransaction.commit();
}
});
button2Id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.re_layout,two);
fragmentTransaction.commit();
}
});
button3Id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.re_layout,three);
fragmentTransaction.commit();
}
});
}
}