Android TestView text Text modification instance

Source: Internet
Author: User

Here we summarize the following about Android TextView text text commonly used two applications, one is like we use will see long files can be folded display, there is a TextView text color textcolor Focus effect, I look at both of these methods.

TextView text Status One, TextView text color TextColor Focus effect

The code is as follows

<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 to select the font color-

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

<!--font color with focus--

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

<!--scrolling selected font Color--

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

<!--default font Color--

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

</selector>

TextView text Status Two, Android TextView text Folding effect

This example to achieve the effect of text unwinding, that is, the default is only 4 lines of text, if the TextView text more than 4 lines, click on the lower right corner of the 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 parcel of two TextView

Layout file Activity_main.xml

Code to copy code as follows

<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

The code is as follows

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 Beijing time July 25 Morning news, in today's new product launch, Google released Android 4.3 version, the code is still" Jelly bean (Jelly bean). it online education platform The new generation of Nexus 7, released today by the Wheat Academy , will be powered by the operating system, and the Nexus Series devices can receive OTA push updates today. A range of features are added to the Android 4.3 operating system. The first is the multi-user provisioning feature, including the "Restricted Files (restricted profiles)" feature for protecting children. Users can limit app content to prevent children from seeing inappropriate content when they use the app, or to contact inappropriate in-app purchases. This feature is similar to the "Children's Playground (Microsoft's Kid's Corner)" feature of Microsoft Windows Phone. The second upgrade is the Bluetooth smart feature, which is Bluetooth low power. ";

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 if the description 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

Android TestView text Text modification instance

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.