SlidingDrawer 자체 적응 내용 너비

먼저 효 과 를 보 세 요. 그림 의 파란색 구역 은 SlidingDrawer 이 고 SlidingDrawer 가 닫 히 며 열 렸 을 때 빨간색 구역 과 녹색 구역 이 자동 으로 적응 합 니 다.
펼 치기 전:
펼 친 후:
참고:http://stackoverflow.com/questions/3654492/android-can-height-of-slidingdrawer-be-set-with-wrap-content
위 주소 에서 말 한 빨간색 구역 은 자동 으로 적응 되 지 않 습 니 다.
빨간색 영역 자동 적응 사고:
간단 하 다 isOpened () 상 태 는 각각 너비 나 높이 를 계산한다.
SlidingDrawer 가 닫 히 거나 열 렸 을 때 requestLayout () 를 다시 호출 하여 layot 폭 을 계산 합 니 다.
다음은 수 정 된 코드 입 니 다.
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>

좋은 웹페이지 즐겨찾기