Android TestView文本文字修改实例
这里我们给大家总结了下关于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文本文字修改实例的更多相关文章
- android从Dialog对话框中取得文本文字
android中Dialog对话框获取文本文字,只需要使用editor的getText方法就可以获得,示例如下:final EditText et = new EditText(this); et.s ...
- Android技术分享-文字转语音并朗读
Android技术分享-文字转语音并朗读 最近在做一个项目,其中有一个功能是需要将文本转换成语音并播放出来.下面我将我的做法分享一下. 非常令人开心的是,Android系统目前已经集成了TTS,提供了 ...
- 怎么在PDF上进行文字修改
文件相信大家不论是工作中还是在学习生活中都会有遇到,有时候我们会遇到PDF文件中的文字有时候会有错误的时候,这个时候就需要对修改PDF文件上的文字,那么具体要怎么做呢,PDF文件需要借助软件才可以编辑 ...
- Android Launcher分析和修改10——HotSeat深入进阶
前面已经写过Hotseat分析的文章,主要是讲解如何在Launcher里面配置以及修改Hotseat的参数.今天主要是讲解一下如何在Hotseat里面的Item显示名称.这个小问题昨天折腾了半天,最后 ...
- Android Launcher分析和修改3——Launcher启动和初始化
前面两篇文章都是写有关Launcher配置文件的修改,代码方面涉及不多,今天开始进入Launcher代码分析. 我们开机启动Launcher,Launcher是由Activity Manager启动的 ...
- Android - 富文本编辑器
Android富文本编辑器(一):基础知识 目前主流的基于Android富文本开发方式思路如下: 基于TextView图文混排 使用方式: TextView textView = new TextVi ...
- (转)完美解决 Android WebView 文本框获取焦点后自动放大有关问题
完美解决 Android WebView 文本框获取焦点后自动放大问题 前几天在写一个项目时,要求在项目中嵌入一个WebView 本来很快就完成了,测试也没有问题.但发给新加坡时,他们测试都会出现文本 ...
- PHP给图片加上图片水印和文字水印实例
下面给大家分享一下PHP给图片加上图片水印和文字水印实例,这也是网站经常用到的功能,把代码加上去,调用就很简单了. 核心代码: function imageWaterMark($groundImage ...
- Android TextView文本处理库推荐
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/115 Android TextView文本处理库推荐 现在 ...
随机推荐
- 帝国cms常用变量总结
一.常用变量 当前栏目ID $GLOBALS['navclassid'] 当前父栏目ID $class_r[$cid]['bclassid'] 栏目路径 $class_r[栏目ID]['classpa ...
- git同步开发更新至项目目录(转载)
From:http://toroid.org/ams/git-website-howto 本地版本库中存放开发的项目代码.以下内容介绍如何将本地版本库修改通过执行“git push web”命令同步到 ...
- [ActionScript 3.0] AS3.0 生成xml方法之一
var type:Array = ["type0", "type1", "type2"]; var property:Array = [[& ...
- [ActionScript 3.0] Away3D 非skybox的全景例子
package { import away3d.containers.View3D; import away3d.controllers.HoverController; import away3d. ...
- node在安装完成后,出现node不是内部或外部命令
node在安装完成后,查看node版本 node -v出现"node不是内部或外部命令"郁闷. 各种搜索之后,处理好了问题了. 一张图解决问题.
- scrum站立会议简介
1简介 站立会议:在敏捷流程的冲刺阶段中,每一天都会举行项目状况会议,强迫每个人向同伴报告进度,迫使大家把问题摆在明面上,这个会议被称为“scrum”或“每日站立会议”. 2.要 ...
- sql游标的使用
转载:http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集 ...
- Android--创建对话框AlertDialog
学习Android过程中发现showDialog().onCreateDialog()这些方法从Android4.0开始都过时了. 官方推荐使用DialogFragment类来创建对话框. 1)布局文 ...
- HTTP与HttpServlet
(1).HTTP协议 Web浏览器和服务器通过HTTP协议在Internet上发送和接收消息.HTTP是一种基于请求/响应模式的协议.客户端发送一个请求,服务器端返回对该请求响应. . (2).HTT ...
- 问题:未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。试图加载格式不正确的程序。
在应用程序池中把对应的高级设置中的启用win32位应用程序改为true即可