Android TextView Some instances of text (text folding/color)

Source: Internet
Author: User
Tags visibility xmlns

TextView text State one , TextView text color TextColor Focus effect

The code is as follows Copy Code

<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 file

<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >

<!--click--> Font Color When selected

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

< font color When the!--has focus-->

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

<!--font Color--> when scrolling selected
<item android:state_selected= "true" android:color= "#FFFFFF"/>

<!--default font color-->

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

</selector>


TextView text State two , Android textview text Folding effect

This example to achieve the effect of text expansion, that is, the default display only 4 lines of text, if TextView text more than 4 lines, click on the lower right corner of more buttons to see all the content. The previous practice is based on the number of words in the TextView to judge, the effect is not very good. Here in a framelayout package of two TextView

Layout file Activity_main.xml

The code is as follows Copy Code

<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 ">

<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 ">&NBSP;

        <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"/>&NBSP;

        <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

The code is as follows Copy Code

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 = "Sina Science and technology news Beijing time July 25, in the morning of the new product launch, Google released the Android 4.3 version, code-named still" jelly beans (Jelly bean). Today's new generation of Nexus 7 will be equipped with the operating system, and Nexus Series devices can receive OTA push updates today. The Android 4.3 operating system adds a range of features. The first is the multiuser Settings feature, including the restricted file (Restricted profiles) feature for child protection. Users can limit the application content to prevent children from seeing inappropriate content when using the application, or to purchase advertisements in inappropriate applications. This feature is similar to the Microsoft Windows Phone's "Children's Park (Microsoft's Kid ' Corner)" feature. The second upgrade is the smart Bluetooth (Bluetooth Smart) feature, the Low-power bluetooth (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;
}
});
}

/**
* Calculate whether the descriptive information is too long
*/
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
}
}

/**
* Change the text of the button "more"
*/
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);
}
}
}

Run effect

is the expanded state

The status after the expansion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.