抽屉效果——抄来的,感觉不错

本文介绍如何在Android中自定义滑动抽屉控件,实现抽屉弹出高度的可控调整。通过修改现有DEMO代码,作者展示了如何根据需求定制抽屉控件,包括其在布局文件中的使用方法和关键代码实现。
抽屉效果的控件在Android里也就是SlidingDrawer,当时讨论需要的是一个可控弹出高度的效果。

通过朋友的一段DEMO的基础上进行了一些改动,完成了一个比较简陋的效果,

由于只是个小DEMO所以有些代码不规范的地方,大家体谅,并可自行修改。测试环境2.1下运行正常,

附上截图若干:


首先自定义一个抽屉控件

package org.widget.drawer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SlidingDrawer;



public class TempSlidingDrawer extends SlidingDrawer {
private boolean mVertical;
private int mTopOffset;


public TempSlidingDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL);
mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0);
mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

final View handle = getHandle();
final View content = getContent();
measureChild(handle, widthMeasureSpec, heightMeasureSpec);

if (mVertical) {
int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;
content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode));
heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();
widthSpecSize = content.getMeasuredWidth();
if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();
}
else {
int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;
getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec);
widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();
heightSpecSize = content.getMeasuredHeight();
if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();
}

setMeasuredDimension(widthSpecSize, heightSpecSize);
}
}



然后在需要的布局文件里添加该自定义控件:(布局可以根据自己的需求自己修改部分)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<org.widget.drawer.TempSlidingDrawer
android:id="@+id/slidingDrawer1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:allowSingleTap="true"
android:animateOnClick="true" android:content="@+id/content"
android:handle="@+id/handle" android:orientation="vertical"
android:background="#000000"
android:layout_alignParentBottom="true">

<Button android:id="@+id/handle" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/btn_menu_top" />

<LinearLayout android:id="@+id/content" android:background="#0000AA"
android:layout_width="wrap_content" android:layout_height="wrap_content">

<TextView android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestTestTest\nTestTestTest\nTestTest\nTestTestTestTest\nTestTestTest\nTestTest\nTest" />
</LinearLayout>
</org.widget.drawer.TempSlidingDrawer>

</RelativeLayout>



在Activity里的代码如下部分

package org.widget.drawer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.SlidingDrawer.OnDrawerScrollListener;

public class MainActivity extends Activity {

private Button btn_drawer;//抽屉的按钮
private TempSlidingDrawer mDrawer;
private Boolean flag = false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initDrawer();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private void initDrawer() {
// TODO Auto-generated method stub
btn_drawer = (Button) findViewById(R.id.handle);
mDrawer = (TempSlidingDrawer) findViewById(R.id.slidingDrawer1);

mDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {

public void onDrawerOpened() {

// TODO Auto-generated method stub
flag = true;
btn_drawer.setBackgroundResource(R.drawable.btn_menu_top);
}
});

mDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {

public void onDrawerClosed() {

// TODO Auto-generated method stub
flag = false;
btn_drawer.setBackgroundResource(R.drawable.btn_menu_top);
}
});

mDrawer.setOnDrawerScrollListener(new OnDrawerScrollListener() {

public void onScrollStarted() {
// TODO Auto-generated method stub

}

public void onScrollEnded() {
// TODO Auto-generated method stub

}
});
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值