这里我们给大家总结了下关于Android TextView文本文字的常用两种应用,一种是像我们使用微信会看到长文件是可以折叠显示了,还有一种就是TextView文字颜色TextColor焦点效果,下面我一起来看这两种方法。

textview文字状态一,TextView文字颜色TextColor焦点效果

代码如下

<TextView

android:id="@+id/tv_quit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="@drawable/list_item_color" />

list_item_color 文件

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- 单击选中时字体颜色-->

<item android:state_pressed="true" android:color="#FFFFFF" />

<!-- 有焦点时的字体颜色-->

<item android:state_focused="true" android:color="#FFFFFF" />

<!-- 滚动选中时字体颜色-->

<item android:state_selected="true" android:color="#FFFFFF" />

<!-- 默认字体颜色-->

<item android:color="#000000" />

</selector>

textview文字状态二,Android TextView文本折叠效果

本例要实现文本展开收起的效果,即默认只显示4行文字,如果textview文字超过4行的话,点击右下角的 更多 按钮即可查看全部的内容。之前的做法是根据 TextView 中的字数来判断,效果不太好。这里在一个FrameLayout 包裹两个 TextView

布局文件 activity_main.xml

代码如下    复制代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="10dp"

tools:context=".MainActivity" >

<”http://www.maiziedu.com”=RelativeLayout xmlns:android>

<TextView

android:id="@+id/tv_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginTop="10dp"

android:layout_marginBottom="10dp"

android:text="@string/textview_fold" />

<FrameLayout

android:id="@+id/fl_desc"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/tv_title"

android:fadingEdge="horizontal"

android:fadingEdgeLength="5dp" >

<TextView

android:id="@+id/tv_desc_short"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:maxLines="4"

android:textColor="@color/black"

android:textSize="16sp" />

<TextView

android:id="@+id/tv_desc_long"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textColor="@color/black"

android:textSize="16sp" />

</FrameLayout>

<Button

android:id="@+id/bt_more"

android:layout_width="50dp"

android:layout_height="25dp"

android:layout_alignParentRight="true"

android:layout_below="@id/fl_desc"

android:layout_marginRight="10dp"

android:background="#1c000000"

android:gravity="center"

android:text="@string/label_more"

android:textSize="15sp"

android:visibility="gone" />

<ImageView

android:id="@+id/iv_more_line"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignBaseline="@id/bt_more"

android:layout_below="@id/fl_desc"

android:layout_toLeftOf="@id/bt_more"

android:background="@drawable/more_line"

android:contentDescription="@string/app_name"

android:visibility="gone" />

</RelativeLayout>

MainActivity.java

代码如下

package com.example.textviewfold;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewTreeObserver;

import android.view.ViewTreeObserver.OnPreDrawListener;

import android.widget.Button;

import android.widget.FrameLayout;

import android.widget.ImageView;

import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

private Button bt_more;

private FrameLayout fl_desc;

private TextView tv_desc_short;

private TextView tv_desc_long;

private boolean isInit = false;

private boolean isShowShortText = true;

private ImageView iv_more_line;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findView();

initView();

setListener();

}

private void setListener() {

bt_more.setOnClickListener(this);

}

private void initView() {

String content = " 新浪科技讯 北京时间7月25日凌晨消息,在今天举行的新产品发布会上,谷歌发布Android 4.3版本,代号仍为"果冻豆 (Jelly Bean)"。IT在线教育平台麦子学院今天发布的新一代Nexus 7将搭载该操作系统,Nexus系列设备今日可收到OTA推送更新。 Android 4.3操作系统新增一系列功能。首先是多用户设置功能,包括针对保护儿童的“受限文件(restricted profiles)” 特性。用户可以对应用内容进行限制,防止儿童在使用应用时看到不适宜内容,或接触不合适的应用内购买广告。这项功能与微软Windows Phone 的"儿童乐园(Microsoft's Kid's Corner)"功能类似。第二项升级是智能蓝牙(Bluetooth Smart)功 能,即"低功耗蓝牙(Bluetooth Low Energy)"。";

tv_desc_short.setText(content);

tv_desc_long.setText(content);

ViewTreeObserver vto = fl_desc.getViewTreeObserver();

vto.addOnPreDrawListener(new OnPreDrawListener() {

@Override

public boolean onPreDraw() {

if (isInit)

return true;

if (mesureDescription(tv_desc_short, tv_desc_long)) {

iv_more_line.setVisibility(View.VISIBLE);

bt_more.setVisibility(View.VISIBLE);

}

isInit = true;

return true;

}

});

}

/**

* 计算描述信息是否过长

*/

private boolean mesureDescription(TextView shortView, TextView longView) {

final int shortHeight = shortView.getHeight();

final int longHeight = longView.getHeight();

if (longHeight > shortHeight) {

shortView.setVisibility(View.VISIBLE);

longView.setVisibility(View.GONE);

return true;

}

shortView.setVisibility(View.GONE);

longView.setVisibility(View.VISIBLE);

return false;

}

private void findView() {

fl_desc = (FrameLayout) findViewById(R.id.fl_desc);

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

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

bt_more = (Button) findViewById(R.id.bt_more);

iv_more_line = (ImageView) findViewById(R.id.iv_more_line);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.bt_more:

if (isShowShortText) {

tv_desc_short.setVisibility(View.GONE);

tv_desc_long.setVisibility(View.VISIBLE);

} else {

tv_desc_short.setVisibility(View.VISIBLE);

tv_desc_long.setVisibility(View.GONE);

}

toogleMoreButton(bt_more);

isShowShortText = !isShowShortText;

break;

default:

break;

}

}

/**

* 更改按钮【更多】的文本

*/

private void toogleMoreButton(Button btn) {

String text = (String) btn.getText();

String moreText = getString(R.string.label_more);

String lessText = getString(R.string.label_less);

if (moreText.equals(text)) {

btn.setText(lessText);

} else {

btn.setText(moreText);

}

}

}

运行效果

Android TestView文本文字修改实例的更多相关文章

  1. android从Dialog对话框中取得文本文字

    android中Dialog对话框获取文本文字,只需要使用editor的getText方法就可以获得,示例如下:final EditText et = new EditText(this); et.s ...

  2. Android技术分享-文字转语音并朗读

    Android技术分享-文字转语音并朗读 最近在做一个项目,其中有一个功能是需要将文本转换成语音并播放出来.下面我将我的做法分享一下. 非常令人开心的是,Android系统目前已经集成了TTS,提供了 ...

  3. 怎么在PDF上进行文字修改

    文件相信大家不论是工作中还是在学习生活中都会有遇到,有时候我们会遇到PDF文件中的文字有时候会有错误的时候,这个时候就需要对修改PDF文件上的文字,那么具体要怎么做呢,PDF文件需要借助软件才可以编辑 ...

  4. Android Launcher分析和修改10——HotSeat深入进阶

    前面已经写过Hotseat分析的文章,主要是讲解如何在Launcher里面配置以及修改Hotseat的参数.今天主要是讲解一下如何在Hotseat里面的Item显示名称.这个小问题昨天折腾了半天,最后 ...

  5. Android Launcher分析和修改3——Launcher启动和初始化

    前面两篇文章都是写有关Launcher配置文件的修改,代码方面涉及不多,今天开始进入Launcher代码分析. 我们开机启动Launcher,Launcher是由Activity Manager启动的 ...

  6. Android - 富文本编辑器

    Android富文本编辑器(一):基础知识 目前主流的基于Android富文本开发方式思路如下: 基于TextView图文混排 使用方式: TextView textView = new TextVi ...

  7. (转)完美解决 Android WebView 文本框获取焦点后自动放大有关问题

    完美解决 Android WebView 文本框获取焦点后自动放大问题 前几天在写一个项目时,要求在项目中嵌入一个WebView 本来很快就完成了,测试也没有问题.但发给新加坡时,他们测试都会出现文本 ...

  8. PHP给图片加上图片水印和文字水印实例

    下面给大家分享一下PHP给图片加上图片水印和文字水印实例,这也是网站经常用到的功能,把代码加上去,调用就很简单了. 核心代码: function imageWaterMark($groundImage ...

  9. Android TextView文本处理库推荐

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/115 Android TextView文本处理库推荐 现在 ...

随机推荐

  1. 深入ThreadLocal之三(ThreadLocal可能引起的内存泄露)

    threadlocal里面使用了一个存在弱引用的map,当释放掉threadlocal的强引用以后,map里面的value却没有被回收.而这块value永远不会被访问到了. 所以存在着内存泄露. 最好 ...

  2. jquery实现的下拉和收缩代码实例

    <!DOCTYPE html>  <html>  <head>  <meta charset=" utf-8">  <meta ...

  3. Python进阶07 函数对象

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象 ...

  4. R中将list类型数据转换成data.frame型

    例如将如下数据转换成data.frame型: l <- replicate( 5, list(sample(letters, 4)), simplify = FALSE ) => 用unl ...

  5. 采访ServiceStack的项目领导Demis Bellot——第1部分(转)

    ServiceStack是一个开源的.支持.NET与Mono平台的REST Web Services框架.InfoQ有幸与Demis Bellot深入地讨论了这个项目.在这篇两部分报道的第1部分中,我 ...

  6. pymongo带认证连接mongo

    import pymongo connection = pymongo.MongoClient("127.0.0.1") connection.database.authentic ...

  7. JDBC数据更新

    在JDBC中通常用Statement类的对象实现对数据库的更新(增.删.查.改)操作 //1.获取数据库连接 connection = getConnection(); //2.准备sql语句 Str ...

  8. OC基础(8)

    自定义代码段 实例变量修饰符 依赖关系 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bott ...

  9. Ogre参考手册(五)3.2 合成器

    3.2 合成器Compositor 合成器框架是Ogre用于全屏后处理的API.你可以通过脚本而不是API定义合成器.你可以很容易为视口实例化合成器. 合成器基础 无论是要替换还是要与主渲染窗口混合, ...

  10. 土法炼钢:怎么实现一个简单的B+Tree In-Disk

    1. 写在前面 说起B+树,大家应该都很熟悉.B+树是一种平衡的多路搜索树,广泛在操作系统和数据库系统用作索引.相比于内存的存取速度,磁盘I/O存取的开销要高上几个数量级.而将B+树用作索引时,它可以 ...