How to create a view similar to the system notification area in android?
I'd like to have a view in my activity, which initially stays at the 开发者_StackOverflowtop of the screen like a little bar, but when you tap on it it should expand down, like the system notification area.
I haven't found any standard controls with such behaviour. What's the best way to implement this?
Use a SlidingDrawer. Here is a good tutorial.
The SlidingDrawer works exactly in this way.
The problem is that the SlidingDrawer can't be positioned at the top of the screen — it opens only upwards (see related question). So I implemented a simple control of my own, using the TranslateAnimation
class MySlidingDrawer extends LinearLayout {
public static final int STATE_OPENED = 0;
public static final int STATE_CLOSED = 1;
private int m_intState;
private LinearLayout m_content;
private ImageButton m_handle;
public MySlidingDrawer(Context context) {
super(context);
setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setOrientation(VERTICAL);
setGravity(Gravity.CENTER);
m_content = new LinearLayout(context);
// add your content here
addView(m_content);
m_intState = STATE_CLOSED;
m_handle = new ImageButton(context);
m_handle.setImageResource(R.drawable.icon);
m_handle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
toggleState();
}
});
m_handle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(m_handle);
}
private int getContentHeight() {
return m_content.getHeight();
}
private void toggleState() {
int intYStart = 0;
int intYEnd = m_intState == STATE_OPENED ? -getContentHeight() : getContentHeight();
Animation a = new TranslateAnimation(0.0f, 0.0f, intYStart, intYEnd);
a.setDuration(1000);
a.setStartOffset(300);
a.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.bounce_interpolator));
startAnimation(a);
m_intState = m_intState == STATE_OPENED ? STATE_CLOSED : STATE_OPENED;
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
offsetTopAndBottom(-getContentHeight()); // content is initially invisible
}
protected void onAnimationEnd() {
super.onAnimationEnd();
int intYOffset = m_intState == STATE_OPENED ? getContentHeight() : -getContentHeight();
offsetTopAndBottom(intYOffset);
}
}
You can use the code posted in this answer: Android SlidingDrawer from top?
The provided solution features setting the orientation of the Slidingdrawer in xml also it's simple requiring only 1 class and some additions in attrs.xml and stable since it's derived from Androids Slidingdrawer from SDK. I also discuss why I didn't choose other popular libs/solutions found on the internet/SO.
Quicklink to the gist: MultipleOrientationSlidingDrawer (source & example) @ gist
精彩评论