1. 程式人生 > >仿知乎日報(3)_MainActivity分析

仿知乎日報(3)_MainActivity分析

MainActivity分析

1、結構

2、介面分析


首先第一張圖片是主頁面,是一個Fragment。第二張圖片是左側滑選單,是一個ListView。整體介面是使用MaterialUI中的DrawableLayout佈局。關於Drawable的使用可以看一下筆者前面的Blog。

3、程式碼

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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/title_bar" />

            <FrameLayout
                android:id="@+id/fl_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></FrameLayout>
        </LinearLayout>
    </FrameLayout>

    <ListView
        android:id="@+id/list_view_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff"
        android:divider="#ffffff"></ListView>


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

4、程式碼分析

最外層框架使用DrawableLayout,接著主介面使用一個FrameLayout用來切換不同的主題,左邊選單欄使用一個ListView,需要注意的是要在ListView中新增 android:layout_gravity=”start” 這個屬性,否則就沒有側滑的效果,其中的start是根據系統語言確定的,按照一般語言從左往右的順序,start就是新增左側滑選單,end就是新增右側滑選單。