Android TestView文本文字修改執行個體

來源:互聯網
上載者:User

標籤:des   android   style   http   color   io   os   ar   使用   

這裡我們給大家總結了下關於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日淩晨訊息,在今天舉行的新產品發布會上,Google發布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文本文字修改執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.