Sad*_*deh 4 layout android textview
我想在textview段落的末尾放置一个按钮,就像"Go"按钮一样,当用户点击它时,应用程序会转到另一个页面.
例如:
if you have good endurance,
for killing the monster you must
going to section 2. [Go->]
-if you haven't good endurance,
flee to section 3. [Go->]
Run Code Online (Sandbox Code Playgroud)
在上面的例子中[Go->]是一个微小的按钮,必须完全放在行尾.
我怎么能在运行时做到这一点?
您可以使用跨度.
我们假设你有一个TextView被叫myText.
Drawable goButtonDrawable = getResources().getDrawable(R.drawable.go_button);
String text = "If you have good endurance, for killing the monster you must go to section 2. [GO]"
String replace = "[GO]";
final int index = text.indexOf(replace);
final int endIndex = index + replace.length();
final ImageSpan imageSpan = new ImageSpan(goButtonDrawable, ImageSpan.ALIGN_BASELINE);
final ClickableSpan clickSpan = new ClickableSpan() {
@Override public void onClick(View clicked) {
// Do your [GO] action
}
};
SpannableString spannedText = new SpannableString(text);
spannedText.setSpan(imageSpan, index, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannedText.setSpan(clickSpan, index, endIndex , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myText.setText(spannedText);
Run Code Online (Sandbox Code Playgroud)
显然,这可以更好地抽象(你可以制作一个内部处理它的自定义TextView),但这是一般的想法.
| 归档时间: |
|
| 查看次数: |
1188 次 |
| 最近记录: |