本文主要是介绍实现TextView内容分块处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现效果图:
这里实现主要是用HorizontalScrollView加TextView实现的,实现布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.a550211.testactivity.MainActivity"><Buttonandroid:id="@+id/btn_add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="添加" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="sdcard>" /><HorizontalScrollViewandroid:id="@+id/scrollView"android:layout_width="match_parent"android:layout_height="wrap_content"android:scrollbars="none"><TextViewandroid:id="@+id/tv_path"android:layout_width="fill_parent"android:layout_height="wrap_content"/></HorizontalScrollView></LinearLayout></LinearLayout>
2、实现HorizontalScrollView初始跳转到最右边
scrollView.post(new Runnable() {public void run() {scrollView.fullScroll(View.FOCUS_RIGHT);}});
3、完整实现代码
package com.example.a550211.testactivity;import android.app.Activity;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.text.style.CharacterStyle;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.StringTokenizer;public class MainActivity extends Activity {private TextView tv_path;private HorizontalScrollView scrollView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();initData();}private void initData() {
// String[] spPath = mCurrentPath.split(Type.ALL_PATH);
// StringTokenizer tokenizer = new StringTokenizer(spPath[1],"/");
// while (tokenizer.hasMoreTokens()){
// sbBuilder.append(tokenizer.nextToken() + " > ");
// }StringBuilder sbBuilder = new StringBuilder();for (int i = 0; i < 10; i++) {sbBuilder.append("username" + i + ">");}String likeUsers = sbBuilder.substring(0, sbBuilder.lastIndexOf(">")).toString();tv_path.setMovementMethod(LinkMovementMethod.getInstance());tv_path.setText(addClickablePart(likeUsers), TextView.BufferType.SPANNABLE);}private SpannableStringBuilder addClickablePart(String str) {SpannableString spanStr = new SpannableString("");SpannableStringBuilder ssb = new SpannableStringBuilder(spanStr);ssb.append(str) ;String[] likeUsers = str.split(">");if (likeUsers.length > 0) {// 最后一个for (int i = 0; i < likeUsers.length; i++) {final String name = likeUsers[i];final int start = str.indexOf(name) + spanStr.length();ssb.setSpan(new ClickableSpan() {@Overridepublic void onClick(View widget) {Toast.makeText(getApplication(), name, Toast.LENGTH_SHORT).show();}@Overridepublic void updateDrawState(TextPaint ds) {super.updateDrawState(ds);ds.setColor(Color.BLACK); // 设置文本颜色// 去掉下划线ds.setUnderlineText(false);}@Overridepublic CharacterStyle getUnderlying() {return super.getUnderlying();}}, start, start + name.length(), 0);}}return ssb.append("");}private void initView() {tv_path = (TextView)findViewById(R.id.tv_path);scrollView = (HorizontalScrollView)findViewById(R.id.scrollView);scrollView.post(new Runnable() {public void run() {scrollView.fullScroll(View.FOCUS_RIGHT);}});}
}
这篇关于实现TextView内容分块处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!