Created
November 30, 2016 12:51
-
-
Save zhEdward/42fad6770376d4735e5590500f745f89 to your computer and use it in GitHub Desktop.
一个 自适应(水平 or 垂直)的 SlidingDrawer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" > | |
<xx.fjnuit.Control.WrappingSlidingDrawer | |
android:id="@+id/SlidingDrawer_fanye" | |
android:layout_width="wrap_content" | |
android:layout_height="fill_parent" | |
android:content="@+id/linearlayout" | |
android:handle="@+id/imageViewIcon" | |
android:orientation="horizontal" > | |
<!-- as well vertical --> | |
<ImageButton | |
android:id="@id/imageViewIcon" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@android:color/transparent" | |
android:src="@drawable/stave_anniu_fanye" /> | |
<LinearLayout | |
android:id="@id/linearlayout" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:paddingTop="5dp"> | |
<!-- stuff you sub view --> | |
</LinearLayout> | |
</xx.fjnuit.Control.WrappingSlidingDrawer> | |
</RelativeLayout> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Description: 自适应slidingDrawer wrap_content | |
* Copyright: Copyright (c)2015-2016 | |
* Company: | |
* @author: Edward | |
* @version: 1.0 | |
* Create at: 2016年11月30日 下午6:10:18 | |
* | |
* Modification History: | |
* Date Author Version Description | |
* ------------------------------------------------------------------ | |
* 2016年11月30日 Edward 1.0 | |
*/ | |
package xx.fjnuit.Control; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.SlidingDrawer; | |
/** | |
* @ref http://stackoverflow.com/a/4265553 | |
* | |
*/ | |
public class WrappingSlidingDrawer extends SlidingDrawer { | |
final String TAG = "WrappingSlidingDrawer"; | |
public WrappingSlidingDrawer(Context context, AttributeSet attrs) { | |
super(context, attrs, 0); | |
// http://stackoverflow.com/a/7913610 | |
int[] textSizeAttr = new int[] { android.R.attr.orientation, | |
android.R.attr.topOffset }; | |
TypedArray a = context.obtainStyledAttributes(attrs, textSizeAttr); | |
mVertical = ORIENTATION_VERTICAL == a.getInt(0, ORIENTATION_VERTICAL); | |
ELFLog.i(TAG, "====" + mVertical); | |
mTopOffset = a.getDimensionPixelSize(1, 0); | |
a.recycle(); | |
} | |
public WrappingSlidingDrawer(Context context, AttributeSet attrs, | |
int defStyle) { | |
super(context, attrs, defStyle); | |
// 该 api 获取 orientation 不正确 | |
// int orientation = attrs.getAttributeIntValue("android", | |
// "orientation", | |
// ORIENTATION_VERTICAL); | |
// mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0); | |
// mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL); | |
// mVertical = false; | |
int[] textSizeAttr = new int[] { android.R.attr.orientation, | |
android.R.attr.topOffset }; | |
TypedArray a = context.obtainStyledAttributes(attrs, textSizeAttr); | |
mVertical = ORIENTATION_VERTICAL == a.getInt(0, ORIENTATION_VERTICAL); | |
ELFLog.i(TAG, "====" + mVertical); | |
mTopOffset = a.getDimensionPixelSize(1, 0); | |
a.recycle(); | |
} | |
@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); | |
if (widthSpecMode == MeasureSpec.UNSPECIFIED | |
|| heightSpecMode == MeasureSpec.UNSPECIFIED) { | |
throw new RuntimeException( | |
"SlidingDrawer cannot have UNSPECIFIED dimensions"); | |
} | |
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); | |
} | |
private boolean mVertical; | |
private int mTopOffset; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment