編輯:關於 Android 程式設計

點擊菜單時可以切換圖示,例如點擊 happy,首頁就會變成一張笑臉,而整個實作過程超級簡單。
你只需要使用 Toolbar 與 DrawerLayout 這兩個較新的控制項。首先要寫三份 XML 版面檔,這裡我用了 include 標籤來嵌入,程式碼如下。
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/tbHeadBar" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/red"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/emotion" android:textColor="@color/white" android:textSize="16sp" /> </android.support.v7.widget.Toolbar>
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/dlMenu" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/llContent" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/ivContent" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/angry" /> </LinearLayout> <!--android:layout_gravity="start"屬性使這部分作為側滑部分--> <!--一定要放在下面!!!關於控件的層次性如果不知道的同學去百度!哦不去谷歌--> <LinearLayout android:id="@+id/llMenu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/white" android:orientation="vertical"> <!--用於設置菜單項--> <ListView android:id="@+id/lvMenu" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.demo.usher.demo_slidingmenu.MainActivity"> <!--頭部--> <include layout="@layout/headbar_toolbar" /> <!--主布局--> <include layout="@layout/my_drawablelayout" /> </LinearLayout>
package com.demo.usher.demo_slidingmenu; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { @BindView(R.id.tbHeadBar) Toolbar mTbHeadBar; /*側滑菜單布局*/ @BindView(R.id.llMenu) LinearLayout mLlMenu; /*側滑菜單ListView放置菜單項*/ @BindView(R.id.lvMenu) ListView mLvMenu; @BindView(R.id.ivContent) ImageView mIvContent; @BindView(R.id.dlMenu) DrawerLayout mMyDrawable; ActionBarDrawerToggle mToggle; private List<String> lvMenuList = new ArrayList<String>() {{ add("angry"); add("happy"); add("sad"); add("embarrassed"); }}; private List<Integer> imageList = new ArrayList<Integer>() {{ add(R.drawable.angry); add(R.drawable.happy); add(R.drawable.sad); add(R.drawable.embarrassed); }}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); /*初始化Toolbar與DrawableLayout*/ initToolBarAndDrawableLayout(); mLvMenu.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, lvMenuList)); mLvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mIvContent.setImageResource(imageList.get(position)); mMyDrawable.closeDrawers();/*收起抽屜*/ } }); } private void initToolBarAndDrawableLayout() { setSupportActionBar(mTbHeadBar); /*以下倆方法設置返回鍵可用*/ getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); /*設置標題文字不可顯示*/ getSupportActionBar().setDisplayShowTitleEnabled(false); mToggle = new ActionBarDrawerToggle(this, mMyDrawable, mTbHeadBar, R.string.open, R.string.close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); //Toast.makeText(MainActivity.this, R.string.open, Toast.LENGTH_SHORT).show(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); //Toast.makeText(MainActivity.this, R.string.close, Toast.LENGTH_SHORT).show(); } }; /*mMyDrawable.setDrawerListener(mToggle);不推薦*/ mMyDrawable.addDrawerListener(mToggle); mToggle.syncState();/*同步狀態*/ } }
ButterKnife 直接在 gradle 檔中設定好即可【缺什麼補什麼】:
apply plugin: 'com.android.application' apply plugin: 'android-apt' android { compileSdkVersion 24 buildToolsVersion "24.0.2" defaultConfig { applicationId "com.demo.usher.demo_slidingmenu" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.2.0' compile 'com.jakewharton:butterknife:8.4.0' /*butterknife相關*/ apt 'com.jakewharton:butterknife-compiler:8.4.0' compile 'com.android.support:support-v4:24.2.0' }
style【返回鍵的顏色樣式等都在 style 檔中修改】:
<resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item> </style> <style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle"> <item name="color">@android:color/white</item> </style> </resources>
很多時候我們在使用第三方控制項時,往往不清楚背後的實作原理。改用原生控制項,能讓我們更透徹地理解某個互動或功能的原理,有助於打造效能與互動都出色的 App。以上就是本文的全部內容,希望對你的工作或學習帶來幫助。
AXML 解析:看懂 Android 二進位 XML 檔案格式
簡述 AXML 即 Android Binary XML,是 Android 應用程式 APK 中儲存 XML 資料的一種方式。它能夠縮小 XML 檔案的體積,也方便系統
Android Monkey 壓力測試完整指南:參數、指令與日誌分析
Monkey 顧名思義就是「猴子」——像一隻猴子坐在電腦前亂敲鍵盤,什麼都不懂,只知道胡亂戳。這正是它的精神:對 App 發送大量隨機使用者事件,看它多久會當掉。因為實作
小米手環怎麼接入微信?開啟步數與排行榜推送
把小米手環接到微信之後,可以直接在微信裡查步數、看排行,不必每次切換 App。設定步驟如下。 先在手机上打开小米手环,点击右上角的「更多」图标。 在選項清單中點擊「服務
Android 6.0 源碼下載、編譯與 Nexus 6P 刷機完整實錄
前言 本文記錄從下載 Android 6.0.1 AOSP 源碼、編譯、處理驅動,到刷入 Nexus 6P 的完整過程。這個過程涉及環境準備、repo 同步、編譯、解鎖、