編輯:關於 Android 程式設計
SlidingDrawer 這個類別如今已被標示為不建議使用,但如果你的效果需求簡單,它其實仍然相當好用。它最大的優點是極簡:只支援上下或左右滑動,幾行程式就能做出抽屜;缺點是初始方向無法透過屬性指定,得靠 orientation 與版面結構自行安排。
SlidingDrawer 必須同時指定兩個子 View:android:handle 是使用者拖曳的「把手」,android:content 則是抽屜拉開後顯示的內容。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000099">
<SlidingDrawer
android:id="@+id/slidingdrawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="vertical" >
<ImageView
android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lo19" />
<LinearLayout
android:id="@id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#008800">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="隱藏的內容" />
</LinearLayout>
</SlidingDrawer>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bottom textView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
寫完配置檔後,Activity 中不寫任何程式碼就能滑動了。
若想針對抽屜的開啟、關閉、滑動過程做處理,可用以下三組 listener:
SlidingDrawer sd = (SlidingDrawer) findViewById(R.id.slidingdrawer);
sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
public void onDrawerOpened() {
// 抽屜已完全開啟
}
});
sd.setOnDrawerCloseListener(new OnDrawerCloseListener() {
public void onDrawerClosed() {
// 抽屜已完全關閉
}
});
sd.setOnDrawerScrollListener(new OnDrawerScrollListener() {
public void onScrollEnded() {
// 滑動結束
}
public void onScrollStarted() {
// 開始滑動
}
});
下面這個例子會把裝置上所有應用程式的圖示,以縮小的方式排列在抽屜中,做法類似早期的桌面啟動器。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SlidingDrawer
android:id="@+id/sliding"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/allApps"
android:handle="@+id/imageViewIcon"
android:orientation="vertical" >
<GridView
android:id="@+id/allApps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/lo13"
android:columnWidth="60dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<ImageView
android:id="@+id/imageViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lo19" />
</SlidingDrawer>
</RelativeLayout>
package com.example.t;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
// 把所有應用程式的圖示縮小,以抽屜方式呈現
public class MainActivity extends Activity {
private GridView gv;
private SlidingDrawer sd;
private ImageView iv;
private List<ResolveInfo> apps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_2);
loadApps();
gv = (GridView) findViewById(R.id.allApps);
sd = (SlidingDrawer) findViewById(R.id.sliding);
iv = (ImageView) findViewById(R.id.imageViewIcon);
gv.setAdapter(new GridAdapter());
// 開抽屜時,把把手圖換成朝下的樣子
sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
iv.setImageResource(R.drawable.lo19);
}
});
// 關抽屜時還原把手圖
sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
iv.setImageResource(R.drawable.lo13);
}
});
}
// 取得所有可啟動的應用程式
private void loadApps() {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
apps = getPackageManager().queryIntentActivities(intent, 0);
}
public class GridAdapter extends BaseAdapter {
public int getCount() {
return apps.size();
}
public Object getItem(int position) {
return apps.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
} else {
imageView = (ImageView) convertView;
}
ResolveInfo ri = apps.get(position);
imageView.setImageDrawable(
ri.activityInfo.loadIcon(getPackageManager()));
return imageView;
}
}
}
這裡的關鍵是 queryIntentActivities():以 ACTION_MAIN 搭配 CATEGORY_LAUNCHER 查詢,就能拿到所有出現在桌面啟動器中的 App 清單,再逐一用 loadIcon() 取出圖示。BaseAdapter 中也示範了 convertView 的重用寫法,避免每次捲動都重新建立 View。
SlidingDrawer 在 API 17 之後被標記為 deprecated,官方建議改用 DrawerLayout(支援 Direction/Navigation Drawer 設計規範)或 BottomSheet。若只是內部工具、原型或需要極簡的抽屜互動,SlidingDrawer 依然可以快速交付;但若是面向一般使用者的正式產品,請優先採用 DrawerLayout,才能獲得手勢衝突處理、系統狀態列配色與無障礙支援等好處。
自訂 Android View 實作:打造可重複使用的 MenuItemView 設定列
在設定頁或選單頁中,常會出現大量重複的列表樣式。若每次都重寫版面,維護成本很高。本文介紹如何自訂 MenuItemView,將常見的設定列封裝成可重複使用的 View。
詳解Android Preference首選項的編寫與組織
翻 Android 書籍時常看到「首選項」這個名詞,但多數書直接教你怎麼用,很少說明它從哪來、憑什麼存在。這一篇就先把概念講清楚,再動手做。 首選項到底是什麼 「首選項」
Android 仿余額寶數字滾動動畫實作
余額寶那種金額數字從 0 快速滾動到最終值的動畫,視覺效果很好,實作起來卻不難。以下是一個可直接複用的自訂 TextView。 效果預覽 完整程式碼 package
用物理計算實現Android擺動球體動畫
效果目標本文實現一個類擺鐘效果的Android動畫:多個帶繩小球並列顯示,左右兩側小球可沿圓弧擺動。與其逐幀製作動畫,不如通過簡單物理計算控制角度變化,讓運動更自然。基礎