这里我们给大家总结了下关于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. Linux Shell入门(转载)

    From:http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用 ...

  2. 跨应用Session共享

    摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...

  3. redis在.net架构中的应用(1)--使用servicestack连接redis(转)

    引言:作为少有的.net架构下的大型网站,stackoverflow曾发表了一篇文章,介绍了其技术体系,原文链接http://highscalability.com/blog/2011/3/3/sta ...

  4. [ActionScript 3.0] 安全沙箱的类型sandboxType,判断当前程序是AIR还是web程序

    表示其中正在运行执行调用的 文件的安全沙箱的类型. Security.sandboxType 具有下列值之一: remote (Security.REMOTE):此文件来自 Internet URL, ...

  5. [ActionScript 3.0] Away3D 天空盒(skybox)例子

    /* SkyBox example in Away3d Demonstrates: How to use a CubeTexture to create a SkyBox object. How to ...

  6. 有没有一行文件字过多后可以省略号显示,我说的不是用其他样式,BT本身有没有?谢谢

    .text-overflow {display: inline-block;max-width: 200px;overflow: hidden;text-overflow: ellipsis;whit ...

  7. jQuery formValidator表单验证插件常见问题

    1.    如何实现一个控件,根据不同的情况,实现不同的控制? 2.    一个页面上我有几个tab页,如何实现每个Tab页上的控件单独校验? 3.    我采用的页面上文字问题的方式,点提交的时候, ...

  8. 程序员必备:Oracle日常维护命令

        上一篇讲了Linux的日常维护命令,这篇讲讲Oracle的日常维护命令.工作中需要使用Oracle数据库的童鞋们,相信或多或少都需要对Oracle做一些基本的维护操作,例如导入导出总该有吧?( ...

  9. 菜鸟-手把手教你把Acegi应用到实际项目中(2)

    上一篇是基于BasicProcessingFilter的基本认证,这篇我们改用AuthenticationProcessingFilter基于表单的认证方式. 1.authenticationProc ...

  10. silverlight,动态数据集合中,移除动态集合自身的内容

    在xaml的页面上创建一个x:Name为_list1的ListBox,其中ListBox里面的每一项是ListBoxItem if (_list1.SelectedItem == null)//如果_ ...