通常情况下,Android的SlidingDrawer控件都需要放到主layout文件中,然后在Activity中findViewById的方式调用.
本方法实现的是SlidingDrawer与主layout文件相分离的方式动态加载显示,思路是通过addContentView的方式动态添加SlidingDrawer.
Activity文件: 点击按钮加载显示SlidingDrawer
package test.zhuzi;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
public class SlidingDrawerTestActivity extends Activity {
private Button buttonToAddDrawer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// LayoutInflater layoutInflater=LayoutInflater.from(this);
// View viewTemp=layoutInflater.inflate(R.layout.drawer,null);
// FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
// FrameLayout.LayoutParams.FILL_PARENT,
// FrameLayout.LayoutParams.WRAP_CONTENT);
// params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
// addContentView(viewTemp, params);
buttonToAddDrawer=(Button)findViewById(R.id.button1);
buttonToAddDrawer.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater layoutInflater=LayoutInflater.from(SlidingDrawerTestActivity.this);
View viewTemp=layoutInflater.inflate(R.layout.drawer,null);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
addContentView(viewTemp, params);
}
});
}
}
自定义的SlidingDrawer控件:
package test.zhuzi;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SlidingDrawer;
public class MyDrawer extends SlidingDrawer {
private boolean mVertical;
private int mTopOffset;
public MyDrawer(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);
}
}
SlidingDrawer的layout布局文件 drawer.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/subLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<test.zhuzi.MyDrawer
android:id="@+id/slidingDrawer1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:allowSingleTap="true"
android:animateOnClick="true"
android:background="#000000"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="vertical" >
<Button
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" />
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 动态添加SlidingDrawer方式\n2 动态添加SlidingDrawer方式\n3 动态添加SlidingDrawer方式\n" />
</LinearLayout>
</test.zhuzi.MyDrawer>
</LinearLayout>
主layout布局文件 main.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>