Android控件之SlidingDrawer(滑動式抽屜)
一、簡介
SlidingDrawer隱藏屏外的內容,并允許用戶通過handle以顯示隱藏內容。它可以垂直或水平滑動,它有倆個View組成,其一是可以拖動的 handle,其二是隱藏內容的View。它里面的控件必須設置布局,在布局文件中必須指定handle和content。例:
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:handle="@+id/slidingDrawerButton"
android:content="@+id/content"
android:background="#ffffff">
<Button
android:id="@+id/slidingDrawerButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00">
</EditText>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:background="#0000ff"
/>
</LinearLayout>
</SlidingDrawer>
二、重要屬性
android:allowSingleTap:指示是否可以通過handle打開或關閉
android:animateOnClick:指示是否當使用者按下手柄打開/關閉時是否該有一個動畫。
android:content:隱藏的內容
android:handle:handle(手柄)
三、重要方法
animateClose():關閉時實現動畫
close():即時關閉
getContent():獲取內容
isMoving():指示SlidingDrawer是否在移動
isOpened():指示SlidingDrawer是否已全部打開
lock():屏蔽觸摸事件
setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener):SlidingDrawer關閉時調用
unlock():解除屏蔽觸摸事件
toggle():切換打開和關閉的抽屜SlidingDrawer
四、完整實例
1.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="100px"
android:background="#0000ff">
</LinearLayout>
<SlidingDrawer
android:id="@+id/slidingDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:handle="@+id/slidingDrawerButton"
android:content="@+id/content"
android:background="#ffffff">
<Button
android:id="@+id/slidingDrawerButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00">
</EditText>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:background="#0000ff"
/>
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
2.
SlidingDrawer.java
package SlidingDrawer.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
public class SlidingDrawer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.widget.SlidingDrawer mDrawer;
final LinearLayout mLinearLayout;
mDrawer=(android.widget.SlidingDrawer)findViewById(R.id.slidingDrawer);
mDrawer.open();
mLinearLayout=(LinearLayout)findViewById(R.id.linearLayout);
mDrawer.setOnDrawerOpenListener(new android.widget.SlidingDrawer.OnDrawerOpenListener()
{
public void onDrawerOpened() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout.getLayoutParams();
linearParams.height=100;
mLinearLayout.setLayoutParams(linearParams);
}
});
mDrawer.setOnDrawerCloseListener(new android.widget.SlidingDrawer.OnDrawerCloseListener(){
public void onDrawerClosed() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mLinearLayout.getLayoutParams();
linearParams.height=400;
mLinearLayout.setLayoutParams(linearParams);
}
});
mDrawer.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
}
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!