日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 点击图片事件,android图文混排点击事件

發布時間:2025/4/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 点击图片事件,android图文混排点击事件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

圖文混排顧名思義就是把文字和圖片混合排列在一起,比較簡單的需求我們也可以通過TextView和ImageView配合使用來達到目的,但是遇到稍微復雜一些的情況這種方法就不適用了。

做這樣一個按鈕:

對于你來說沒有任何難度:LinearLayout+TextView+ImageView搞定;或者可以直接使用TextView的drawableLeft屬性。

這里記錄兩種復雜情況的處理方法:

為部分文字添加點擊事件;

圖文混排,圖片居中,圖片可點擊。

1. 為部分文字添加點擊事件

我最終實現的效果是這個樣子的:

可點擊文字顯示為特殊顏色,并可以點擊。個人覺得這個最大的特色就是可以像普通文本一樣換行,這是組合控件所不能實現的。

這里使用了SpannableString和ClickableSpan實現,具體代碼如下:

ClickSpan繼承自ClickableSpan:

import android.text.TextPaint;

import android.text.style.ClickableSpan;

import android.view.View;

import com.travis.uqmei.utils.ToastUtil;

/**

* Created by travis on 16/9/18.

*/

public class ClickSpan extends ClickableSpan {

private String txt;

public ClickSpan(String txt){

this.txt = txt;

}

@Override

public void onClick(View widget) {

String content = String.format("ClickSpan is clicked, and txt is %s ",txt);

ToastUtil.show(content);

}

@Override

public void updateDrawState(TextPaint ds) {

//根據自己的需求定制文本的樣式

ds.setColor(ds.linkColor);

ds.setUnderlineText(false);

}

}

為TextView設置部分文字可點擊效果:

TextView tv = (TextView) findViewById(R.id.tv);

String from = "張全蛋";

String to = "趙鐵柱";

String txt = String.format("%s回復@%s:我是富士康3號流水線的張全蛋," +

"英文名叫Micheal Jack,發文名叫helodie Jaqueline。", from, to);

SpannableString span = new SpannableString(txt);

ClickSpan clickSpan = new ClickSpan(to);

span.setSpan(clickSpan, txt.indexOf(to),

txt.indexOf(to) + to.length(),

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

tv.setText(span);

tv.setMovementMethod(LinkMovementMethod

.getInstance());

2. 圖文混排,圖片居中,圖片可點擊

效果如下:

可點擊效果通過上述ClickSpan實現,圖文混排通過VerticalImageSapn實現。

VerticalImageSpan繼承自ImageSpan:

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Rect;

import android.graphics.drawable.Drawable;

import android.text.style.ImageSpan;

/**

* 垂直居中的ImageSpan

*

* @author travis

*/

public class VerticalImageSpan extends ImageSpan {

public VerticalImageSpan(Drawable drawable) {

super(drawable);

}

public int getSize(Paint paint, CharSequence text, int start, int end,

Paint.FontMetricsInt fontMetricsInt) {

Drawable drawable = getDrawable();

Rect rect = drawable.getBounds();

if (fontMetricsInt != 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;

fontMetricsInt.ascent = -bottom;

fontMetricsInt.top = -bottom;

fontMetricsInt.bottom = top;

fontMetricsInt.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 drawable = getDrawable();

canvas.save();

int transY = 0;

transY = ((bottom - top) - drawable.getBounds().bottom) / 2 + top;

canvas.translate(x, transY);

drawable.draw(canvas);

canvas.restore();

}

}

代碼設置:

TextView tv = (TextView) findViewById(R.id.tv);

String icon = "icon";

String from = "張全蛋"+icon;

String to = "趙鐵柱";

String txt = String.format("%s回復@%s:我是富士康3號流水線的張全蛋," +

"英文名叫Micheal Jack,發文名叫helodie Jaqueline。", from, to);

//設置ClickSpan,為部分文字("icon")添加點擊效果

SpannableString span = new SpannableString(txt);

ClickSpan clickSpan = new ClickSpan(icon);

span.setSpan(clickSpan, txt.indexOf(icon),

txt.indexOf(icon) + icon.length(),

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

//設置ImageSpan,占用可點擊文字("icon")的位置

Bitmap bitmap = ImageUtils.resize(BitmapFactory.decodeResource(getResources(),

R.mipmap.uqmei_icon_contact), DensityUtil.sp2px(this, 12f));

BitmapDrawable drawable = new BitmapDrawable(bitmap);

drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());

span.setSpan(new VerticalImageSpan(drawable),

txt.indexOf(icon), txt.indexOf(icon) + icon.length(),

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//設置TextView

tv.setText(span);

tv.setHighlightColor(Color.TRANSPARENT);//消除點擊時的背景色

tv.setMovementMethod(LinkMovementMethod.getInstance());

總結

以上是生活随笔為你收集整理的android 点击图片事件,android图文混排点击事件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。