textview文字状态一,TextView文字颜色TextColor焦点效果
代码如下android:id="@+id/tv_quit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/list_item_color" /> list_item_color 文件 本例要实现文本展开收起的效果,即默认只显示4行文字,如果textview文字超过4行的话,点击右下角的 更多 按钮即可查看全部的内容。之前的做法是根据 TextView 中的字数来判断,效果不太好。这里在一个FrameLayout 包裹两个 TextView 布局文件 activity_main.xml <frameLayout
android:id="@+id/bt_more" MainActivity.java package com.example.textviewfold; import android.app.Activity; public class MainActivity extends Activity implements OnClickListener { private Button bt_more; @Override findView(); private void setListener() { private void initView() { ViewTreeObserver vto = fl_desc.getViewTreeObserver(); /** private void findView() { bt_more = (Button) findViewById(R.id.bt_more); @Override tv_desc_short.setVisibility(View.GONE); default: /** String text = (String) btn.getText(); 运行效果 为展开的状态 展开后的状态 android studio|Android TextView文本文字一些实例(文本折叠/颜色) http://m.bbyears.com/wangyetexiao/47538.html
textview文字状态二,Android TextView文本折叠效果
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity" >
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" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:fadingEdge="horizontal"
android:fadingEdgeLength="5dp" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="4"
android:textColor="@color/black"
android:textSize="16sp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="16sp" />
</frameLayout>
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" />
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" />
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;
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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}
bt_more.setOnClickListener(this);
}
String content = " 新浪科技讯 北京时间7月25日凌晨消息,在今天举行的新产品发布会上,谷歌发布Android 4.3版本,代号仍为"果冻豆 (Jelly Bean)"。今天发布的新一代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);
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;
}
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);
iv_more_line = (ImageView) findViewById(R.id.iv_more_line);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_more:
if (isShowShortText) {
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;
break;
}
}
* 更改按钮【更多】的文本
*/
private void toogleMoreButton(Button btn) {
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);
}
}
}