編輯:關於 Android 程式設計
新聞類 App 的主介面幾乎都長得差不多:頂部一條帶 logo 與選單的 ActionBar,左側可滑出的抽屜放功能入口。要做出這個效果,需要四塊拼圖——DrawerLayout 側邊欄、ActionBar 的樣式與 logo、menu 選單資源,以及負責雙向動畫的 ActionBarDrawerToggle。動工前請記得先把 theme 換成帶 ActionBar 的主題,例如 android:theme="@android:style/Theme.Holo.Light"。

依官方文件,DrawerLayout 的第一個子視圖是 Activity 的主內容,第二個才是側邊欄。因此主布局用 FrameLayout 承載內容,再用 include 引入左側抽屜,並以 layout_gravity="start" 指定它從左側出現。
<android.support.v4.widget.DrawerLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/mdrawlaout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffeeeeee">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是主界面"/>
</FrameLayout>
<include
android:layout_width="180dp"
android:layout_height="match_parent"
layout="@layout/drawer_layout_left"
android:layout_gravity="start"
android:clickable="true" />
</android.support.v4.widget.DrawerLayout>
抽屜內容用 CircleImageView 這個開源元件做圓形頭像,屬性設定很直覺,再往下排四個文字選單即可。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:background="#ffffff">
<!-- 圓形頭像 -->
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="https://schemas.android.com/apk/res-auto"
android:id="@+id/circleimageview"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="30dp"
android:src="@drawable/circlel_header"
app:border_color="#ff0000"
app:border_width="2dp" />
<!-- 選單列表 -->
<TextView
android:id="@+id/tv_item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center"
android:text="夜間工具"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="繪畫工具"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="測試1"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_item4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="測試2"
android:textSize="16sp" />
</LinearLayout>
ActionBar 的細項多半得靠程式碼設定:隱藏標題、啟用 logo、換上 logo 圖與背景,並把 App 圖示轉為可點擊的返回鈕。
// 取得 ActionBar ActionBar actionBar = getActionBar(); // 不顯示標題文字 actionBar.setDisplayShowTitleEnabled(false); // 啟用 logo 顯示 actionBar.setDisplayUseLogoEnabled(true); actionBar.setLogo(R.drawable.netease_top); // 設定背景 Drawable background = getResources().getDrawable(R.drawable.top_bar_background); actionBar.setBackgroundDrawable(background); // 將 App 圖示轉為可點擊的返回按鈕 actionBar.setDisplayHomeAsUpEnabled(true);
背景色則交給 XML 定義,用 shape 包一層 solid 即可日後換色。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="@color/top_title_bar_normal_backgrond_color"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="gray">#bbbbbb</color>
<color name="black">#000000</color>
<color name="top_title_bar_normal_backgrond_color">#EB413D</color>
<color name="top_title_bar_button_press_background_color">#D83C38</color>
</resources>
如此一來 ActionBar 就會呈現網易紅。
修改 menu_main.xml 即可,Android Studio 產生的 onCreateOptionsMenu() 會自動解析它。這裡的技巧是把「更多」做成一個可展開的子選單,讓 overflow 裡也能有圖示與文字。
<menu xmlns:android="https://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_settings"
android:icon="@drawable/night_biz_pc_menu_icon"
android:orderInCategory="1"
android:title="@string/app_name"
android:showAsAction="always"/>
<item
android:id="@+id/action_btn01"
android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
android:orderInCategory="2"
android:title="更多"
android:showAsAction="always">
<menu>
<item android:id="@+id/action_btn02" android:icon="@drawable/biz_plugin_manage_weather"
android:orderInCategory="100" android:title="11/13" android:showAsAction="never"/>
<item android:id="@+id/action_btn03" android:icon="@drawable/biz_plugin_manage_offline"
android:orderInCategory="100" android:title="離線" android:showAsAction="never"/>
<item android:id="@+id/action_btn04" android:icon="@drawable/biz_plugin_manage_theme"
android:orderInCategory="100" android:title="夜間" android:showAsAction="never"/>
<item android:id="@+id/action_btn05" android:icon="@drawable/biz_plugin_manage_search"
android:orderInCategory="100" android:title="搜索" android:showAsAction="never"/>
<item android:id="@+id/action_btn06" android:icon="@drawable/biz_plugin_manage_qrcode"
android:orderInCategory="100" android:title="掃一掃" android:showAsAction="never"/>
<item android:id="@+id/action_btn07" android:icon="@drawable/biz_plugin_manage_offline"
android:orderInCategory="100" android:title="設置" android:showAsAction="never"/>
</menu>
</item>
</menu>
ActionBarDrawerToggle 是一個開關,用來開啟或關閉抽屜。它把 DrawerLayout 與 ActionBar 綁在一起,點擊 ActionBar 上的 Home 按鈕就能彈出抽屜,滑動抽屜時左上角還會跟著變形。使用時要在兩個回呼中轉發事件:
onConfigurationChanged:處理螢幕旋轉等配置變更onOptionsItemSelected:攔截 Home 按鈕的點擊另外必須在 onPostCreate() 中呼叫 syncState(),讓 toggle 的狀態與抽屜同步,並把漢堡圖示設為 ActionBar 的 Home 圖示。
// 設定 DrawerLayout 的點擊事件
mdrawlayout.setDrawerListener(new MyAPPdrawerlistener());
// 建立抽屜開關
mActionBarDrawerToggle = new ActionBarDrawerToggle(
this, this.mdrawlayout, R.string.drawer_open, R.string.drawer_close);
/**
* 該方法會自動與 ActionBar 關聯,把開關圖示顯示在 ActionBar 上。
* 若不設定,抽屜功能仍可用,只是會使用預設圖示。
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}
// 設備配置改變時(例如旋轉螢幕)
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
// 交給 toggle 判斷是否為 Home 按鈕點擊
return mActionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
最後在 DrawerLayout 的監聽器中把四個回呼轉發給 toggle,抽屜滑動時圖示就會跟著旋轉。
private class MyAPPdrawerlistener implements DrawerLayout.DrawerListener {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
mActionBarDrawerToggle.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerOpened(View drawerView) {
mActionBarDrawerToggle.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
mActionBarDrawerToggle.onDrawerClosed(drawerView);
}
@Override
public void onDrawerStateChanged(int newState) {
mActionBarDrawerToggle.onDrawerStateChanged(newState);
}
}
1. android:paddingLeft 與 android:layout_marginLeft:前者是「內容距容器左邊的距離」,後者是「整個 View 距父容器左邊的距離」。
2. android:gravity 決定控制項「內部內容」的對齊方式,android:layout_gravity 決定控制項「自身」在父容器中的位置。
3. android:orderInCategory 是 ActionBar 中每個 item 的排序權重,數值越大越靠後,位置不足時會被擠進 overflow。
4. 在 Android Studio 中引入 GitHub 上的函式庫,直接把原始碼複製進專案,再在 gradle 中加上對方提供的依賴敘述即可。
Android 視頻錄製常見問題整理
在 Android 中實作視頻錄製時,常會遇到流程設計、預覽顯示、清晰度、檔案大小與影片方向等問題。本文整理常見環節,提供開發時的參考方向。 錄製流程 以常見的短視頻錄製
Android 入門實作:打造可點擊的 Button
不論是為了日後的工作需求,還是眼下的專案,掌握行動開發都是一項值得投資的技能。而學習任何新技術,總得從最經典的 Hello World 開始,Android 自然也不例外
用 emulator -data 參數從 CMD 啟動 Android 模擬器
在命令列(CMD)中使用 emulator.exe 啟動 Android 模擬器有兩種方式: ・emulator -avd(AVD 名稱) ・emulator -data
Android 屬性動畫深入解析:Evaluator、Interpolator 與 ViewPropertyAnimator
1. Evaluator 自定義 1)Evaluator 介紹 使用 ValueAnimator 時,常會透過 ofInt、ofFloat 或 ofObject 建立動畫