上一篇文章记录了 ToolBar 控件的使用方法。
下面对它进行封装,以便以后的重复使用
1、完成布局文件 layout_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义view"
android:layout_gravity="center"
/>
</android.support.v7.widget.Toolbar>
</LinearLayout>
2、完成 ToolBarLayout.class
public class ToolBarLayout extends LinearLayout {
private Context context;
private Toolbar toolbar;
public ToolBarLayout(Context context) {
super(context);
this.context = context;
init();
}
public ToolBarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
private void init() {
LayoutInflater inflater=LayoutInflater.from(context);
// LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.layout_toolbar,this);//注意,此处为 this,而非null
toolbar = (Toolbar)findViewById(R.id.mytoolbar);
toolbar.setNavigationIcon(R.mipmap.list);
toolbar.setTitle("主标题");
toolbar.setTitleTextColor(Color.BLUE);
toolbar.setSubtitle("subtitle");
toolbar.setSubtitleTextColor(Color.YELLOW);
toolbar.inflateMenu(R.menu.base_toolbar_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_search:
Toast.makeText(getContext(),"item_search",Toast.LENGTH_SHORT).show();
break;
case R.id.item_send:
Toast.makeText(getContext(),"item_send",Toast.LENGTH_SHORT).show();
break;
case R.id.item_alarm:
Toast.makeText(getContext(),"item_alarm",Toast.LENGTH_SHORT).show();
break;
case R.id.item_plane:
Toast.makeText(getContext(),"item_plane",Toast.LENGTH_SHORT).show();
break;
case R.id.item1:
Toast.makeText(getContext(),"item1",Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(getContext(),"item2",Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"navigation onclick",Toast.LENGTH_SHORT).show();
}
});
}
}
3、封装
3.1 完成 activity_toolbar.xml ,引用封装好的view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<com.example.zhan.test.ToolBarLayout
android:id="@+id/toolbarlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.zhan.test.ToolBarLayout>
</LinearLayout>
3.2 编写 ToolBarActivity.class
public class ToolBarActivity extends AppCompatActivity {
private Toolbar toolbar;
private ToolBarLayout toolBarLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏系统标题栏
setContentView(R.layout.activity_toolbar);
toolBarLayout= (ToolBarLayout) findViewById(R.id.toolbarlayout);
}
}
至此,完成
问题笔记:
1、layoutInflater.inflate(resourceId, root);
inflate()方法一般接收两个参数,第一个参数就是要加载的布局id
第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null (摘自:Android LayoutInflater原理分析,带你一步步深入了解View(一))
2、public View inflate( int resource, ViewGroup root, boolean attachToRoot)
解析:
@param resource :ID for an XML layout resource to load 需要加载布局文件的id,意思是将这个布局文件加载到Activity中里操作
@param root :Optional view to be the parent of the generated hierarchy(层次) (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
@param attachToRoot :Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
如果attachToRoot 为 true:并且root存在,将xml挂载到root下,返回root
false:返回xml的根布局