Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

一步一步學 Android 控件(二十七):SlidingDrawer

編輯:關於 Android 程式設計

官方對 SlidingDrawer 的解釋是:它會把內容隱藏在螢幕之外,並允許使用者拖動 handle 把內容拉出到螢幕上。一個 SlidingDrawer 由兩個子 View 組成——一個「handle」(把手)與一個「content」(內容)。要注意的是,SlidingDrawer 在 API 17 中已被棄用。

以下透過一個簡單範例來學習這個控件。先來看效果圖:

1. 佈局檔 widget_sliding_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/sliding_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:content="@+id/content"
    android:handle="@+id/handle" >
    <ImageView android:id="@+id/handle"
        android:layout_width="15dip"
        android:layout_height="50dip"
        android:src="@drawable/seek_bar_style" />
    <LinearLayout android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <ImageView android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/seek_bar_thum" />
    </LinearLayout>
</SlidingDrawer>

這裡用到了 seek_bar_style 與 seek_bar_thum,具體定義請參考「一步一步學 Android 控件(二十五)—— SeekBar」。

2. Activity:WidgetsSlidingDrawerActivity

Activity 部分只加上一句 setContentView:

package com.xy.zt.selfdefinewieget;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class WidgetsSlidingDrawerActivity extends Activity implements OnTouchListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widget_sliding_drawer);
    }
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
}

3. 在 ViewData.java 中註冊(此部分可選)

public static final int SLISING_DRAWER_ID = QUICK_CONTACT_BADGE_ID + 1;
public static final String SLIDING_DRAWER_NAME = "SlidingDrawer";
private static final ViewData mSlidingDraer = new ViewData(SLIDING_DRAWER_NAME, SLISING_DRAWER_ID);
View_Datas.add(mSlidingDraer);

並在 WidgetsAdapter 的 handleItemClicked 中補上:

case ViewData.SLISING_DRAWER_ID:
    intent.setClass(mContext, WidgetsSlidingDrawerActivity.class);
    mContext.startActivity(intent);
    break;

以上就是 SlidingDrawer 控件的全部內容,下一個控件將介紹 ViewFlipper。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved