自定义ToolBar
- 之前学习的ToolBar都是V7包下的,自从AndroidX推出后,尝试使用这个版本下的Toolbar
第一步:屏蔽掉系统的ActionBar
<resources>
<!-- Base application theme. -->
<!--屏蔽掉ActionBar-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!--这个是ActionBar的颜色-->
<item name="colorPrimary">@color/colorPrimary</item>
<!--这个是顶部状态栏的颜色-->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!--这个是控件选中的颜色-->
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
第二步:自定义ToolBar布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/toolbar"
android:minHeight="?attr/actionBarSize">
</androidx.appcompat.widget.Toolbar>
- 关于 android:minHeight="?attr/actionBarSize"这个属性,稍加留意。定义好之后,将其添加到你需要的布局文件中
<include layout="@layout/my_toolbar"/>
第三步:定义menu菜单
- 首先,要在res文件夹下新建menu文件夹,之后创建menu布局文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/a_search"
android:orderInCategory="80"
android:title="搜索"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/a_share"
android:orderInCategory="90"
android:title="分享"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/a_setting"
android:orderInCategory="100"
android:title="设置"
app:showAsAction="never"/>
</menu>
- android:orderInCategory:控制选项的顺序,数值越小,优先级越高
- app:showAsAction:定义显示的方式,always(可以一直看到)、never(隐藏在"..."之中),ifRoom(有位置就显示,否则就会隐藏在"..."之中)
第四步:加载menu+toolbar
- 重写onCreateOptionsMenu方法,来加载menu布局
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout,menu);
return true;
}
- 初始化控件
// TODO: 注意导包别导错了
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_public_black_24dp);
toolbar.setTitle("自定义Toolbar");
setSupportActionBar(toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.a_share:
Toast.makeText(MainActivity.this,"a_share",Toast.LENGTH_LONG).show();
break;
case R.id.a_setting:
Toast.makeText(MainActivity.this,"a_setting",Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
});
}
}
更复杂的用法可以自己定义;
抽屉布局
- 关于抽屉布局,在布局文件中,有以下要注意的点:
1、DrawerLayout包含主界面和侧滑界面;
2、主界面布局一定要位于所有侧滑界面布局之前,宽高应设置为match_parent且不能包含layout_gravity标签
3、侧滑界面必须设置layout_gravity属性,左侧滑出(start/left),右侧滑出(end/right);
4、侧滑界面高度建议设定为match_parent,宽度可以设定为一个定值;
5、每个界面边缘最多允许设置一个侧滑界面,否则会报错;
相关示例如下:
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主界面-->
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/ic_launcher_background"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="内容界面"
android:gravity="center"
android:textColor="@android:color/white"
/>
</LinearLayout>
<!--侧滑界面-->
<LinearLayout
android:id="@+id/ll_tabs"
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/darker_gray"
android:layout_gravity = "start"
>
<TextView
android:id="@+id/tv_close"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:gravity="center"
android:text="侧滑界面,点击收回侧滑"
android:textColor="@android:color/white" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
- 相关代码调用,ActionBarDrawerToggle + DrawerLayout 对侧滑动作进行监听是常用组合,
drawerLayout = findViewById(R.id.drawer_layout);
//ActionBarDrawerToggle + DrawerLayout 对侧滑动作进行监听
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,
R.string.drawer_open,
R.string.drawer_close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
//侧滑状态同步
mDrawerToggle.syncState();
//设置侧滑监听
drawerLayout.setDrawerListener(mDrawerToggle);
TextView close = findViewById(R.id.tv_close);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//侧滑界面在左边,右边为RIGHT
drawerLayout.closeDrawer(Gravity.LEFT);
}
});
抽屉布局是比较常用的布局,在此处做个记录,方便自己以后查询和使用。
网友评论