From 347d4c8ab4e0fc5b4e9795eb879c5151db8d72d2 Mon Sep 17 00:00:00 2001 From: Jamling Date: Mon, 17 Apr 2017 18:27:47 +0800 Subject: [PATCH] fix filtertab issue --- .../cn/ieclipse/af/view/FilterTabView.java | 2 +- .../af/demo/common/view/SimpleFilterTab.java | 76 +++++++++++++++++++ .../res/layout/common_filter_tab_item.xml | 20 +++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 sample/src/main/java/cn/ieclipse/af/demo/common/view/SimpleFilterTab.java create mode 100644 sample/src/main/res/layout/common_filter_tab_item.xml diff --git a/library/src/cn/ieclipse/af/view/FilterTabView.java b/library/src/cn/ieclipse/af/view/FilterTabView.java index 7e77d81..d8ad1ff 100644 --- a/library/src/cn/ieclipse/af/view/FilterTabView.java +++ b/library/src/cn/ieclipse/af/view/FilterTabView.java @@ -43,7 +43,7 @@ public FilterTabView(FilterTabHost expandableLayout) { } public FilterTabView(FilterTabHost expandableLayout, CharSequence title) { - this(expandableLayout, null, null); + this(expandableLayout, title, null); } public FilterTabView(FilterTabHost expandableLayout, CharSequence title, OnPopupItemClickListener listener) { diff --git a/sample/src/main/java/cn/ieclipse/af/demo/common/view/SimpleFilterTab.java b/sample/src/main/java/cn/ieclipse/af/demo/common/view/SimpleFilterTab.java new file mode 100644 index 0000000..5c39052 --- /dev/null +++ b/sample/src/main/java/cn/ieclipse/af/demo/common/view/SimpleFilterTab.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2015-2016 adviser2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cn.ieclipse.af.demo.common.view; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.support.annotation.RequiresApi; +import android.text.TextUtils; +import android.util.AttributeSet; +import android.widget.ToggleButton; + +/** + * Description + * + * @author Jamling + */ +public class SimpleFilterTab extends ToggleButton { + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + public SimpleFilterTab(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + public SimpleFilterTab(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public SimpleFilterTab(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public SimpleFilterTab(Context context) { + super(context); + } + + @Override + protected void onDraw(Canvas canvas) { + Drawable[] drawables = getCompoundDrawables(); + if (drawables != null) { + Drawable drawableLeft = drawables[2]; // right drawable + if (drawableLeft != null) { + CharSequence text = getText(); + if (TextUtils.isEmpty(text)) { + text = getHint(); + } + float textWidth = 0; + if (!TextUtils.isEmpty(text)) { + textWidth = getPaint().measureText(text.toString()); + } + int drawablePadding = getCompoundDrawablePadding(); + int drawableWidth = 0; + drawableWidth = drawableLeft.getIntrinsicWidth(); + float bodyWidth = textWidth + drawableWidth + drawablePadding; + float middle = (getMeasuredWidth() - bodyWidth) / 2; + if (middle > 0) { + canvas.translate(-middle, 0); + } + } + } + super.onDraw(canvas); + } +} diff --git a/sample/src/main/res/layout/common_filter_tab_item.xml b/sample/src/main/res/layout/common_filter_tab_item.xml new file mode 100644 index 0000000..bff3bb4 --- /dev/null +++ b/sample/src/main/res/layout/common_filter_tab_item.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file