SlidingDrawer 自适应内容宽度

本文介绍如何实现SlidingDrawer在打开和关闭时,其内容区域(红色和绿色部分)能够自动适应宽度。通过在`onMeasure`方法中根据`isOpened()`状态计算不同宽度,并在SlidingDrawer状态改变时调用`requestLayout()`来更新布局。参考了Stack Overflow上的解决方案。

先看下效果 ,图中蓝色区域为SlidingDrawer,SlidingDrawer关闭,打开时,红色区域和绿色区域自动适应。

展开前:



展开后:



参考了: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>


评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值