Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
####先看大众点评的购买须知
#### 先看大众点评的购买须知
![大众.png](http://upload-images.jianshu.io/upload_images/3523210-e7971f5aa1049148.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
######如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用 android:drawableLeft 来做,可做完发现:当TextView内容为单行的时候是没有问题的,多行的时候,添加的这个drawableLeft就随着TextView高度而垂直居中了。
######一般解决办法嵌套布局解决,将(Textview)文字 与(ImageView)小圆点分离,这肯定是不符合布局优化。
######前面写过一篇文章[Android 优化布局(解决TextView drawableLeft/top/right布局中大小不可控的方法)](http://www.jianshu.com/p/d3027acf475a)主要针对TextView drawable属性的大小进行了控制,这次在原来的基础上通过设置drawable. setBounds(int left, int top, int right, int bottom)位置进行了控制达到下面的效果:
###### 如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用 android:drawableLeft 来做,可做完发现:当TextView内容为单行的时候是没有问题的,多行的时候,添加的这个drawableLeft就随着TextView高度而垂直居中了。
###### 一般解决办法嵌套布局解决,将(Textview)文字 与(ImageView)小圆点分离,这肯定是不符合布局优化。
###### 前面写过一篇文章[Android 优化布局(解决TextView drawableLeft/top/right布局中大小不可控的方法)](http://www.jianshu.com/p/d3027acf475a)主要针对TextView drawable属性的大小进行了控制,这次在原来的基础上通过设置drawable. setBounds(int left, int top, int right, int bottom)位置进行了控制达到下面的效果:

![123.png](http://upload-images.jianshu.io/upload_images/3523210-ae55e5504b7aae6e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

######也可以达到图标大小可控的效果
###### 也可以达到图标大小可控的效果

![000.png](http://upload-images.jianshu.io/upload_images/3523210-0ad048c82ce9bad8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#####下面看看使用方法
######step1:Add it in your root build.gradle at the end of repositories:
##### 下面看看使用方法
###### step1:Add it in your root build.gradle at the end of repositories:
````
allprojects {
repositories {
Expand All @@ -19,32 +19,33 @@ allprojects {
}
}
````
######step2:Add the dependency
###### step2:Add the dependency
````
dependencies {
compile 'com.github.ithedan:TextViewDrawable:v1.0'
}
````
#####图标与第一行文字对齐 xml (app:isAliganCenter="false"默认是true)
##### 图标与第一行文字对齐 xml (app:isAliganCenter="false"默认是true)
````
<com.hedan.textdrawablelibrary.TextViewDrawable
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/dot_blue"
android:drawablePadding="10dp"
android:includeFontPadding="false"
android:padding="10dp"
android:text="孩童100-140CM半价,100CM以下收60元餐费。本团购使用时间为周一到周五的晚餐以及周末的午餐!每天限接待30份\n团购用户不可同时享受其他优惠"
android:textColor="#000000"
app:isAliganCenter="false"
/>
````
#####图标与第一行文字对齐 activity
##### 图标与第一行文字对齐 activity
````
TextViewDrawable tv4=(TextViewDrawable)findViewById(R.id.text4);
tv4.setText("提供免费WIFI\n停车位收费标准:详情咨询商家");
````
#####图标大小 xml
##### 图标大小 xml
````
<com.hedan.textdrawablelibrary.TextViewDrawable
android:padding="10dp"
Expand All @@ -57,11 +58,12 @@ TextViewDrawable tv4=(TextViewDrawable)findViewById(R.id.text4);
app:drawableLeftHeight="30dp"
android:drawableRight="@drawable/iconfont_youjiantou"
app:drawableRightWidth="15dp"
android:includeFontPadding="false"
app:drawableRightHeight="20dp"
android:textColor="#000000"
android:gravity="center_vertical"
android:text="首页"
android:textSize="20sp"
/>
````
####如有什么问题,敬请提出,十分感谢!希望越来越好,谢谢!如果喜欢,还请点击start,喜欢支持一下了,谢谢O(∩_∩)O~
#### 如有什么问题,敬请提出,十分感谢!希望越来越好,谢谢!如果喜欢,还请点击start,喜欢支持一下了,谢谢O(∩_∩)O~
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

排除fontpadding影响

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class TextViewDrawable extends AppCompatTextView {

private int drawableLeftWidth, drawableTopWidth, drawableRightWidth, drawableBottomWidth;
private int drawableLeftHeight, drawableTopHeight, drawableRightHeight, drawableBottomHeight;
private boolean isAliganCenter=true;
private boolean isDwMath_content=false;
private boolean isAliganCenter = true;
private boolean isDwMath_content = false;
private int mWidth, mHeight;

public TextViewDrawable(Context context) {
Expand Down Expand Up @@ -51,50 +51,83 @@ private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
//
Drawable[] drawables = getCompoundDrawables();
Drawable drawableLeft = drawables[0];
Drawable drawableTop = drawables[1];
Drawable drawableRight = drawables[2];
Drawable drawableBottom = drawables[3];
if (drawableLeft != null) {
setDrawable(drawableLeft, 0, drawableLeftWidth, drawableLeftHeight);
}
if (drawableTop != null) {
setDrawable(drawableTop, 1, drawableTopWidth, drawableTopHeight);
}
if (drawableRight != null) {
setDrawable(drawableRight, 2, drawableRightWidth, drawableRightHeight);
}
if (drawableBottom != null) {
setDrawable(drawableBottom, 3, drawableBottomWidth, drawableBottomHeight);
this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
}

@Override
public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
//
if (mWidth != 0 && mHeight != 0) {
if (left != null) {
setDrawable(left, 0, drawableLeftWidth, drawableLeftHeight);
}
if (top != null) {
setDrawable(top, 1, drawableTopWidth, drawableTopHeight);
}
if (right != null) {
setDrawable(right, 2, drawableRightWidth, drawableRightHeight);
}
if (bottom != null) {
setDrawable(bottom, 3, drawableBottomWidth, drawableBottomHeight);
}
}
this.setCompoundDrawables(drawableLeft,drawableTop,drawableRight,drawableBottom);
//
super.setCompoundDrawables(left, top, right, bottom);
}

private void setDrawable(Drawable drawable, int tag, int drawableWidth, int drawableHeight) {
// 首先要有数据
if (getLineCount() == 0 || getLineCount() == 1) {
return;
}
//获取图片实际长宽
int width = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth;
int height = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight;
int mDrawableWidth = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth;
int mDrawableHeight = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight;
// view 的高度
int mViewHeight = getHeight() - getPaddingBottom() - getPaddingTop();
// 行数、行高、行数*行高
int mLineCount = getLineCount();
int mLineHeight = getLineHeight();
int mTextHeight = mLineCount * mLineHeight;
// 每行的offset
int mLineOffset = 0;
if (mViewHeight > mTextHeight) {
mLineOffset = (mViewHeight - mTextHeight) / mLineCount;
}
// 最终行高
int mFinalLineHeight = mLineHeight + mLineOffset;
//
int left = 0, top = 0, right = 0, bottom = 0;
switch (tag) {
case 0:
case 2:
// 中心点向上移动的 行 (mLineCount - 1) / 2.0f
// 1行 (1 - 1) / 2.0f) = 0 不移动
// 2行 (2 - 1) / 2.0f) = 0.5 移动0.5行
// 3行 (3 - 1) / 2.0f) = 1 移动1行
int moveTop = -(int) (((mLineCount - 1) / 2.0f) * mFinalLineHeight);
//
left = 0;
top = isAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
right = width;
bottom = top + height;
top = isAliganCenter ? 0 : moveTop;
right = mDrawableWidth;
bottom = top + mDrawableHeight;
break;
case 1:
left =isAliganCenter ? 0: -mWidth/2+width/2;
left = isAliganCenter ? 0 : -mWidth / 2 + mDrawableWidth / 2;
top = 0;
right = left+width;
bottom =top+height;
right = left + mDrawableWidth;
bottom = top + mDrawableHeight;
break;
}
drawable.setBounds(left, top, right, bottom);
Expand Down