Toolbar使用
(1) 默认Toolbar中的title、logo都是居左的,如果要改成居中,只能自定义View放到Toolbar中
(2) 如果Toolbar单独文件且和CoordinatorLayout结合,则layout_scrollFlags必须和CoordinatorLayout在同一个文件中,不能写在Layout中。
(3) 在Activity设置
Theme.AppCompat.Light.NoActionBar
不要在Toolbar上设置
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
2. Toolbar中Navigation使用
mToolbar.setNavigationIcon(R.drawable.icon_titlebar_usercenter);
点击事件处理
mToolbar.setNavigationOnClickListener(NOnClickListener(View.OnClickListener() { (View v) { gStartActivity(SettingActivity.)}}))
上面的方式不好用,不知道什么原因,放在下面代码的前后都不行
setSupportActionBar(mToolbar);
要使用
public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { gStartActivity(SettingActivity.) } return super.onOptionsItemSelected(item)}
3. Toolbar上使用SearchView
如何监听键盘上点击搜索按钮的事件
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu) final SearchView searchView = (SearchView) MenuItemCompat.(menu.findItem(R.id.action_search)) searchView.setOnQueryTextListener(SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { Bundle bundle = Bundle() bundle.putString(SearchActivity.KEY_SEARCH_CONTENT, query) gStartActivity(SearchActivity.class, bundle) searchView.clearFocus() }}
3. TabLayout和ViewPager
ViewPager.setAdapter必须在TabLayout.setupWithViewPager前,否则报错
给Adapter设置数据必须在TabLayout.setupWithViewPager()前,否则TabLayout.getTabCount()返回0
updateTabData(0, 0, 0);mTabLayout.setupWithViewPager(mViewPager);int tabCount = mTabLayout.getTabCount();
TabLayout的tab切换监听方法,如果使用setupWithViewPager会创建默认的Listener,如果开发者再添加Listenter,会导致点击tab切换时,ViewPager不会切换。坑啊。
setOnTabSelectedListener()public void setupWithViewPager(@NonNull ViewPager viewPager) { final PagerAdapter adapter = viewPager.getAdapter(); if (adapter == null) { throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set"); } // First we'll add Tabs, using the adapter's page titles setTabsFromPagerAdapter(adapter); // Now we'll add our page change listener to the ViewPager viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(this)); // Now we'll add a tab selected listener to set ViewPager's current item setOnTabSelectedListener(new ViewPagerOnTabSelectedListener(viewPager)); // Make sure we reflect the currently set ViewPager item if (adapter.getCount() > 0) { final int curItem = viewPager.getCurrentItem(); if (getSelectedTabPosition() != curItem) { selectTab(getTabAt(curItem)); } }}