Android實戰簡單新聞客戶端(1)---主界面設計

模仿實例地址:SimpleNews


前言這是github開源項目,原項目有四個模塊。我這裏之模仿其中
一個(新聞)。學會MD開發和MVP架構。


主頁佈局:

這裏寫圖片描述

這裏主要使用了toolbar和DrawerLayout實現導航條以及側滑的效果。
當然爲了填充側滑欄,又使用了NavigationView這個控件。

toolbar和DrawerLayout

我已經在前面的博客文章介紹過了:
android–UI—側滑drawerLayout
android–UI–導航條toolbar
NavigationView實踐
使用ToolBar代替ActionBar的時候,首先去掉Actionbar
修該主題:

<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>

爲了使用NavigationView,添加依賴:

    compile 'com.android.support:design:24.2.0'

新人要知道如何搜索依賴添加:File–>ProjectStructure–>app–>Dependencies–>綠色加號–>Library Dependencies–>搜索–>雙擊
這裏寫圖片描述

佈局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".Activity.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include
            android:id="@+id/appbar"
            layout="@layout/toolbar" />

        <!-- Content -->
        <FrameLayout
            android:id="@+id/content_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/appbar" />
    </RelativeLayout>


    <!-- Drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_head"
        app:menu="@menu/drawer" />

</android.support.v4.widget.DrawerLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="5dp"
         />
</LinearLayout>

menu文件,給側邊欄添加選項。以及頂部layout

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_original"
            android:checked="true"
            android:icon="@mipmap/ic_launcher"
            android:title="菜單1"/>

        <item
            android:id="@+id/navigation_library"
            android:icon="@mipmap/ic_launcher"
            android:title="菜單2"/>
    </group>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="192dp"
    android:background="?attr/colorPrimaryDark"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">
    
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="lw_zhaoritian"
        android:gravity="center"
       />
</LinearLayout>

效果測試:
這裏寫圖片描述
toolbar還是空的,需要在代碼裏設置。

MainActivity:

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private Toolbar mToolbar;
    private NavigationView mNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open,
                R.string.close);
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mNavigationView = (NavigationView) findViewById(R.id.navigation);
        setupDrawerContent(mNavigationView);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            Toast.makeText(getApplicationContext(),
                 item.getTitle(), Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        Toast.makeText(getApplicationContext(),
                                menuItem.getTitle(), Toast.LENGTH_LONG).show();
                        menuItem.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
    }
}

測試:
這裏寫圖片描述

講解:
首先:獲取當前的toolbar取代之前的actionbar

mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

第一步:給toolbar添加menu並且添加事件----詳見 android–UI–導航條toolbar

第二步:
使用ActionBarDrawerToggle類,使我們的toolbar和drawer更好的顯示。
參考文章:ActionBarDrawerToggle和ToolBar結合使用

ActionBarDrawerToggle就是DrawerLayout事件的監聽器。

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(getActivity(),  
                drawerLayout, toolBar, R.string.drawer_open, R.string.drawer_close){  
            @Override  
            public void onDrawerOpened(View drawerView) {  
                super.onDrawerOpened(drawerView);  
                if (!mUserLearnedDrawer && !mFromSavedInstanceState) {  
                    mUserLearnedDrawer = true;  
                    saveToPreferences(getActivity(), KEY_USER_LEARNED_DWARER, mUserLearnedDrawer + "");  
                }  
                getActivity().invalidateOptionsMenu();  
            }  
  
            @Override  
            public void onDrawerClosed(View drawerView) {  
                super.onDrawerClosed(drawerView);  
                getActivity().invalidateOptionsMenu();  
            }  
  
            @Override  
            public void onDrawerSlide(View drawerView, float slideOffset) {  
                if (slideOffset < 0.6) {  
                    toolBar.setAlpha(1 - slideOffset);  
                }  
            }  
        };  

實例化ActionBarDrawerToggle時候三個參數:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open,
                R.string.close);
        mDrawerToggle.syncState();

設置DrawerLayout監聽:

mDrawerLayout.setDrawerListener(mDrawerToggle);

設置側滑菜單的點擊事件:

private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        Toast.makeText(getApplicationContext(),
                                menuItem.getTitle(), Toast.LENGTH_LONG).show();
                        menuItem.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章