|
現在工具夠多了,
所以接下來在把之前的
在如何使用Toolbar-Menu
如何使用DrawerLayout
如何使用下拉更新(SwipeRefreshLayout)
如何使用TabLayout
全部組合在一起。
main.xml
- <android.support.v4.widget.DrawerLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/drawer_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <!-- The main content view -->
- <android.support.v4.widget.SwipeRefreshLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/refresh_layout">
- <RelativeLayout
- android:id="@+id/content_frame"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <android.support.v7.widget.Toolbar
- android:layout_alignParentTop="true"
- android:id="@+id/toolbar"
- android:background="#fff000"
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:minHeight="?attr/actionBarSize" />
- <android.support.design.widget.TabLayout
- android:layout_below="@id/toolbar"
- android:id="@+id/tabs"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <android.support.v4.view.ViewPager
- android:id="@+id/viewpager"
- android:layout_below="@id/tabs"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />
- </RelativeLayout>
- </android.support.v4.widget.SwipeRefreshLayout>
- <!-- The navigation drawer -->
- <ListView android:id="@+id/left_drawer"
- android:layout_width="240dp"
- android:layout_height="match_parent"
- android:layout_gravity="start"
- android:choiceMode="singleChoice"
- android:divider="@android:color/transparent"
- android:dividerHeight="0dp"
- android:background="#99999999"/>
- </android.support.v4.widget.DrawerLayout>
複製代碼
MainActivity.java
- public class MainActivity extends AppCompatActivity {
- private DrawerLayout mDrawerLayout;
- private ActionBarDrawerToggle mActionBarDrawerToggle;
- private Toolbar toolbar;
- private android.support.design.widget.TabLayout mTabs;
- private ViewPager mViewPager;
- private SwipeRefreshLayout mSwipeRefreshLayout;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initToolbar();
- initViewPager();
- initSwipRefresh();
- }
- private void initSwipRefresh(){
- mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
- mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- mSwipeRefreshLayout.setRefreshing(false);
- }
- });
- }
- private void initToolbar(){
- toolbar = (Toolbar) findViewById(R.id.toolbar);
- // Set an OnMenuItemClickListener to handle menu item clicks
- toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
- @Override
- public boolean onMenuItemClick(MenuItem item) {
- // Handle the menu item
- return true;
- }
- });
- // Inflate a menu to be displayed in the toolbar
- toolbar.inflateMenu(R.menu.menu_main);
- setSupportActionBar(toolbar);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- getSupportActionBar().setHomeButtonEnabled(true);
- mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
- mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
- @Override
- public void onDrawerOpened(View drawerView) {
- super.onDrawerOpened(drawerView);
- }
- @Override
- public void onDrawerClosed(View drawerView) {
- super.onDrawerClosed(drawerView);
- }
- };
- mActionBarDrawerToggle.syncState();
- mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
- }
- private void initViewPager(){
- mTabs = (android.support.design.widget.TabLayout) findViewById(R.id.tabs);
- mTabs.addTab(mTabs.newTab().setText("Tab 1"));
- mTabs.addTab(mTabs.newTab().setText("Tab 2"));
- mTabs.addTab(mTabs.newTab().setText("Tab 3"));
- mViewPager = (ViewPager) findViewById(R.id.viewpager);
- mViewPager.setAdapter(new SamplePagerAdapter());
- mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabs));
- }
- private class SamplePagerAdapter extends PagerAdapter {
- @Override
- public int getCount() {
- return 3;
- }
- @Override
- public boolean isViewFromObject(View view, Object o) {
- return o == view;
- }
- @Override
- public CharSequence getPageTitle(int position) {
- return "Item " + (position + 1);
- }
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- View view = getLayoutInflater().inflate(R.layout.pager_item,
- container, false);
- container.addView(view);
- TextView title = (TextView) view.findViewById(R.id.item_title);
- title.setText(String.valueOf(position + 1));
- return view;
- }
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
- container.removeView((View) object);
- }
- }
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu_main, menu);
- MenuItem menuSearchItem = menu.findItem(R.id.my_search);
- // Get the SearchView and set the searchable configuration
- SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
- // SearchView searchView = (SearchView) menuSearchItem.getActionView();
- SearchView searchView = (SearchView) menuSearchItem.getActionView();
- // Assumes current activity is the searchable activity
- searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
- searchView.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.action_about:
- // About option clicked.
- return true;
- case R.id.action_exit:
- // Exit option clicked.
- return true;
- case R.id.action_settings:
- // Settings option clicked.
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- }
複製代碼
pager_item.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:gravity="center">
- <TextView
- android:id="@+id/item_subtitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Page:"/>
- <TextView
- android:id="@+id/item_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="80sp" />
- </LinearLayout>
- manifest
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="example.givemepass.toolbardemo" >
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:theme="@style/AppTheme"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <!-- Searchable -->
- <activity
- android:name=".SearchableActivity"
- android:theme="@style/Theme.AppCompat.Light"
- android:launchMode="singleTop" >
- <intent-filter>
- <action android:name="android.intent.action.SEARCH" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- </intent-filter>
- <meta-data
- android:name="android.app.searchable"
- android:resource="@xml/searchable"
- />
- </activity>
- <!-- Points to searchable activity so the whole app can invoke search. -->
- <meta-data
- android:name="android.app.default_searchable"
- android:value=".SearchableActivity" />
- </application>
- </manifest>
- style
- <resources>
- <!-- Base application theme. -->
- <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
- <!-- Customize your theme here. -->
- </style>
- </resources>
- menu
- <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_settings" android:title="@string/action_settings"
- android:orderInCategory="100" app:showAsAction="never" />
- <item
- android:id="@+id/action_about"
- android:orderInCategory="200"
- app:showAsAction="never"
- android:title="About"/>
- <item
- android:id="@+id/action_exit"
- android:orderInCategory="300"
- app:showAsAction="never"
- android:title="Exit"/>
- <item android:id="@+id/my_search"
- android:title="@string/search_title"
- android:icon="@drawable/ic_open_search"
- app:showAsAction="ifRoom"
- app:actionViewClass="android.support.v7.widget.SearchView" />
- </menu>
複製代碼
gradle
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- compile 'com.android.support:appcompat-v7:22.2.0'
- compile 'com.android.support:design:22.2.0'
- }
複製代碼 程式碼
文章來源
|
|