抽屉效果的控件在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
}
});
}
}
抽屉效果——抄来的,感觉不错
猜你喜欢
转载自worm.iteye.com/blog/1715045
今日推荐
周排行