TextView篇
1.TextView上面使用代码设置drawableLeft或者drawableRight。这个属性一般用于Text文本较短的情况。
在xml中我们经常会为了简洁布局,在TextView中使用这两个属性。但是在有些情况下,列如点赞操作,TextView里面关联的这个图片属性要根据字段发生改变,这时候需要在代码中修改:
Drawable leftDrawable = getResources().getDrawable(R.drawable.icon_upvo_ed);
leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
tv_praise.setCompoundDrawables(leftDrawable, null, null, null);
2.TextView后面要接上图片,而这个TextView的文本相当长,效果如下:
这时候如果使用drawableRight来接后面这个“动态”,效果却是这样的:
这显然不是我们需要的效果。
要实现这个需求可以在代码中使用HTML来实现:
holder.tv_content.append(Html.fromHtml("<img src='"+R.drawable.icon_vipcircle_xu+"'/>", getter, null));
ImageGetter getter=new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable=getResources().getDrawable(Integer.parseInt(source));
//自适应图片大小,可以自行修改
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
return drawable;
}
};
这里图片的大小用drawable.setBounds(10, 0, 40, 25)来控制。
如果是在文本前面设置图片,如下:
可以下这么设置:
String html = "<img src='" + R.drawable.icon_voucher_return + "'/>";//标题左侧返券图片
CharSequence charSequence = Html.fromHtml(html, new VoucherImageGetter(), null);
viewHolder.tvVoucherTittle.setText(charSequence);
//然后再原来的charSequence的基础上追加文本
viewHolder.tvVoucherTittle.append(" 双十一葡萄特惠返券双十一葡萄特惠返券");
VoucherImageGetter类:
private class VoucherImageGetter implements Html.ImageGetter {
public VoucherImageGetter() {
}
@Override
public Drawable getDrawable(String source) {
Drawable drawable = context.getResources().getDrawable(Integer.parseInt(source));
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
return drawable;
}
}
结果如下:
可以看到效果已经出来了,可是还是有点问题,那就是TextView左边的图片跟第二行紧贴在一起了,这时候你可能会觉得,这个bug小case,设置一下行距就行了,比如设置1.3倍的行距:
android:lineSpacingMultiplier="1.3"
结果出来的时候你会大失所望,因为行距是有了,可是还是紧紧贴在一起。我修改了好几个属性还是一样。这时候我又找了另外的一个api,发现还有其他的方法可以实现,主要原理还是使用CharSequence进行。代码如下:
private void setTextViewDrawble(TextView tv,final int drawble,String textContent) {
SpannableStringBuilder spannableString = new SpannableStringBuilder();
spannableString.append(textContent);
VerticalImageSpan imageSpan = new VerticalImageSpan(context,drawble);
//也可以这样
//Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
//drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//ImageSpan imageSpan1 = new ImageSpan(drawable);
//将index为6、7的字符用图片替代
spannableString.setSpan(imageSpan, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(spannableString);
}
重新ImageSpan,重新修改size:
package com.lianjiu.b.common.utils;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.style.ImageSpan;
/**
* 功能描述:
* 作者:Linxz
* E-mail:lin_xiao_zhang@13.com
* 版本信息:V1.0.0
* 时间:2017年04月10日 14:53.
**/
public class VerticalImageSpan extends ImageSpan{
public VerticalImageSpan(Context arg0, int arg1) {
super(arg0, arg1);
}
public int getSize(Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fm) {
Drawable d = getDrawable();
Rect rect = d.getBounds();
if (fm != null) {
Paint.FontMetricsInt fmPaint=paint.getFontMetricsInt();
int fontHeight = fmPaint.bottom - fmPaint.top;
int drHeight=rect.bottom-rect.top;
int top= drHeight/2 - fontHeight/4;
int bottom=drHeight/2 + fontHeight/4;
fm.ascent=-bottom;
fm.top=-bottom;
fm.bottom=top;
fm.descent=top;
}
return rect.right;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
Drawable b = getDrawable();
canvas.save();
int transY = 0;
transY = ((bottom-top) - b.getBounds().bottom)/2+top;
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
}
布局里面同时设置行距大小:
android:lineSpacingMultiplier="1.3"
结果如下:
可以看到图片跟下一行之间已经有间隔了,这里的效果还是不太工整,调整一下重写的类即可。
3.TextView的text文本中,有时候需要制定的位置设置不一样的字体。列如:
这个布局中的评论可以用三个TextView构成,回复+回复的人+回复内容,但是可以用一个TextView那布局就更简洁了。
TextView在Java代码中:
//sp是:回复+回复人+回复内容
SpannableString sp = new SpannableString("回复" + item.getParentUser().getNickname() + ":" + item.getContent());
//设置:“回复”颜色
sp.setSpan(new
ForegroundColorSpan(mcontext.getResources().getColor(R.color.tv_hint)), 0, 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//设置:"回复人"颜色
sp.setSpan(new ForegroundColorSpan(mcontext.getResources().getColor(R.color.yellow)), 2, (2 + item.getParentUser().getNickname().length() + 1), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//设置:"回复内容"颜色
sp.setSpan(new ForegroundColorSpan(mcontext.getResources().getColor(R.color.tv_hint)), (2 + item.getParentUser().getNickname().length()) + 1, sp.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
viewHolder.tv_evaluationContent.setText(sp);
4.TextView中有中划线,像打折的金额一样,如:
在Java代码中实现:
tv_charged.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
5.TextView设置跑马灯效果
(1).重写TextView
public MarqueeText(Context con) {
super(con);
}
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
}
(2).写属性是text内容的长度一定要大过android:layout_width的长度
<com.lcx.widget.MarqueeText
android:layout_centerInParent="true"
android:singleLine="true"
android:textColor="@color/red"
android:text="这才是真正的文字跑马灯效果这才是真正的文字跑马灯效果"
android:id="@+id/tv_songName2"
android:layout_width="200px"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true" />
6.EditTextView(为TextView子类)被软键盘遮挡住的解决办法
在activity
super.onCreate(savedInstanceState);
setContentView(R.layout.item_praise);
之前添加:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
7.在开发的过程当中有可能会在TextView里面添加特殊字符串,如书名号《》,或者为了对齐需要缩进两个字符,这时候直接在布局很难实现,往往都需要在string.mxl或者java代码中实现(转载):
http://mikewang.blog.51cto.com/3826268/862643/
TextView属性大全
http://blog.csdn.net/lovexjyong/article/details/6852533