先看下效果 ,图中蓝色区域为SlidingDrawer,SlidingDrawer关闭,打开时,红色区域和绿色区域自动适应。
展开前:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmLwgTMy81N5QzM5UDN1MTMvwFNw8CXyEjMxAjMvw1ckF2bsBXdvwFdl5mLuR2cj5Set1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
展开后:
参考了:http://stackoverflow.com/questions/3654492/android-can-height-of-slidingdrawer-be-set-with-wrap-content
上面地址所说的,红色区域不会自动适应。
红色区域自动适应思路:
很简单,在onMeasure 方法中根据 isOpened()状态,分别计算不同宽度或者高度。
在SlidingDrawer关闭或者打开时重新调用requestLayout() 计算layout宽高。
下面是经过修改的代码:
package com.eshion.soft.tms;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.SlidingDrawer;
/**
*
* wrap size for SlidingDrawer
*
*/
public class WrapSlidingDrawer extends SlidingDrawer implements SlidingDrawer.OnDrawerOpenListener,SlidingDrawer.OnDrawerCloseListener{
private boolean mVertical;
private int mTopOffset;
private final static boolean DEBUG = false;
private final static String TAG = "WrapSlidingDrawer";
public WrapSlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
int orientation = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "orientation", ORIENTATION_VERTICAL);
if(DEBUG) Log.e(TAG, "222-orientation=" + orientation);
mTopOffset = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "topOffset", 0);
mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
}
public WrapSlidingDrawer(Context context, AttributeSet attrs) {
super(context, attrs);
int orientation = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "orientation", ORIENTATION_VERTICAL);
if(DEBUG) Log.e(TAG, "222-orientation=" + orientation);
mTopOffset = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "topOffset", 0);
mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL);
setOnDrawerOpenListener(this);
setOnDrawerCloseListener(this);
}
@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));
if(isOpened()){
heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();
} else {
heightSpecSize = handle.getMeasuredHeight() + mTopOffset;
}
// heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight();
widthSpecSize = content.getMeasuredWidth();
if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth();
} else {
int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;
content.measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec);
if(isOpened()){
widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();
} else {
widthSpecSize = handle.getMeasuredWidth() + mTopOffset ;
}
//widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth();
heightSpecSize = content.getMeasuredHeight();
if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight();
}
setMeasuredDimension(widthSpecSize, heightSpecSize);
}
@Override
public void onDrawerClosed() {
if(DEBUG) Log.e(TAG, "onDrawerClosed"+ "-isOpen?=" + isOpened() +"-getParent()=" + getParent());
requestLayout();
}
@Override
public void onDrawerOpened() {
if(DEBUG) Log.e(TAG, "onDrawerOpened"+ "-isOpen?=" + isOpened());
requestLayout();
}
}
xml写法:
<LinearLayout
android:id="@+id/content_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/img_switcher"
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#FF0000"
android:layout_weight="1">
</LinearLayout>
<include
android:id="@+id/settings_drawer"
layout="@layout/settings_drawer"/>
</LinearLayout>
settings_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.eshion.soft.tms.WrapSlidingDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:handle="@+id/sliding_button"
android:background="#0000FF"
android:orientation="horizontal"
android:content="@+id/settings_panel">
<ImageView
android:id="@id/sliding_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_search"/>
<LinearLayout
android:id="@id/settings_panel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00"
>
<TextView
style="@style/SettingsItem"
android:background="@drawable/ic_launcher"/>
<TextView
style="@style/SettingsItem"
android:background="@drawable/ic_launcher"/>
<TextView
style="@style/SettingsItem"
android:background="@drawable/ic_launcher"/>
<TextView
style="@style/SettingsItem"
android:background="@drawable/ic_launcher"/>
<TextView
style="@style/SettingsItem"
android:background="@drawable/ic_launcher"/>
<TextView
style="@style/SettingsItem"
android:background="@drawable/ic_launcher"/>
<TextView
style="@style/SettingsItem"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
</com.eshion.soft.tms.WrapSlidingDrawer>