Toolbar使用java
(1) 默認Toolbar中的title、logo都是居左的,若是要改爲居中,只能自定義View放到Toolbar中android
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/common_color_white"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="@drawable/icon_back"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="center"
android:src="@drawable/icon_titlebar_logo"/>
</android.support.v7.widget.Toolbar>
(2) 若是Toolbar單獨文件且和CoordinatorLayout結合,則layout_scrollFlags必須和CoordinatorLayout在同一個文件中,不能寫在Layout中。app
<!-- layout_scrollFlags屬性不能放在layout裏,不然隱藏很差用 -->
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_toolbar"
app:elevation="@dimen/default_elevation"
app:layout_scrollFlags="scroll|enterAlways"
/>
(3) 在Activity設置ide
Theme.AppCompat.Light.NoActionBar
不要在Toolbar上設置this
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
2. Toolbar中Navigation使用 spa
mToolbar.setNavigationIcon(R.drawable.icon_titlebar_usercenter);
點擊事件處理
code
mToolbar.setNavigationOnClickListener(NOnClickListener(View.OnClickListener() { (View v) { gStartActivity(SettingActivity.)} }))
上面的方式很差用,不知道什麼緣由,放在下面代碼的先後都不行xml
setSupportActionBar(mToolbar);
要使用
事件
public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { gStartActivity(SettingActivity.) } return super.onOptionsItemSelected(item) }
3. Toolbar上使用SearchView開發
<menu 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"
tools:context=".MainActivity" >
<item
android:id="@+id/action_search"
android:title="action_search"
android:icon="@drawable/icon_titlebar_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
如何監聽鍵盤上點擊搜索按鈕的事件
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)); } } }