前言
最近正在优化公司的项目,很多东西需改进。例如BaseActivity的封装,这里简单记录一下toolbar的封装,当然BaseActivity还需要封装一些其他操作,以后会贴出自己的代码,这里就不做过多记录...
正文
内容比较简单就直接上代码;
如果不是特殊情况需要增加别的东西,就是这么多了;
第一步:依赖 appcompat-v7;
compile 'com.android.support:appcompat-v7:25.2.0'
第二步:设置 NoActionBar 主题:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
第三步:创建 TopBarBaseActivity与其布局文件;
1,封装的TopBarBaseActivity.java
public abstract class TopBarBaseActivity extends AppCompatActivity {
private Toolbar toolbar;
private FrameLayout viewContent;
private TextView mTitle;
private int menuId;
private String menuStr;
private OnClickListener onClickListenerTopLeft; //左边图标的点击事件
private OnClickListener onClickListenerTopRight;
//定义接口
public interface OnClickListener {
void onClick();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_bar_base);
toolbar = (Toolbar)findViewById(R.id.toolbar);
viewContent = (FrameLayout)findViewById(R.id.view_content);
mTitle = (TextView)findViewById(R.id.tv_title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater.from(this).inflate(getContentView(),viewContent);
init(savedInstanceState);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onClickListenerTopLeft.onClick();
}else if(item.getItemId() == R.id.menu_1) {
onClickListenerTopRight.onClick();
}
return true; //自己处理点击事件
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (menuId != 0 || !TextUtils.isEmpty(menuStr)) {
getMenuInflater().inflate(R.menu.menu_activity_base_top_bar,menu);
}
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (menuId != 0) {
menu.findItem(R.id.menu_1).setIcon(menuId);
}
if (!TextUtils.isEmpty(menuStr)) {
menu.findItem(R.id.menu_1).setTitle(menuStr);
}
return super.onPrepareOptionsMenu(menu);
}
protected void setTopRightButton(String menuStr,OnClickListener rightListener) {
this.onClickListenerTopRight = rightListener;
this.menuStr = menuStr;
}
protected void setTopRightButton(String menuStr,int menuId, OnClickListener rightListener) {
this.menuStr = menuStr;
this.menuId = menuId;
this.onClickListenerTopRight = rightListener;
}
//添加一个方法设置图标资源id和监听器
protected void setTopLeftButton(int iconId,OnClickListener listener) {
toolbar.setNavigationIcon(iconId);
this.onClickListenerTopLeft = listener; //接口回调
}
protected void setTitle(String title) {
if (!TextUtils.isEmpty(title)) {
mTitle.setText(title);
}
}
protected abstract int getContentView();
protected abstract void init(Bundle saveInstanceState);
}
2,activity_top_bar_base.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.gyq.topbarfast.TopBarBaseActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@android:color/white"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/view_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
3,menu_activity_base_top_bar.xml布局文件:
<?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/menu_1"
android:title=""
app:showAsAction = "always"/>
</menu>
第四步:如何使用?继承这个TopBarBaseActivity 即可:
public class MainActivity extends TopBarBaseActivity {
@Override
protected int getContentView() {
return R.layout.activity_main;
}
@Override
protected void init(Bundle saveInstanceState) {
setTitle("直播吧");
setTopLeftButton(R.drawable.ic_title_back, new OnClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this,"你点击了左上角的图标",Toast.LENGTH_SHORT).show();
}
});
setTopRightButton("我的", R.drawable.ic_mine, new OnClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this,"你点击了右上角的图标",Toast.LENGTH_SHORT).show();
}
});
}
}
这样,基本实现需求。
网友评论