Create a textview that can be expandabletextview with open source projects

Source: Internet
Author: User

This time or open-source projects to achieve the effect, I personally feel that the above effect is still great. So it is quite simple to record how to achieve this effect. This is a good example of open source projects, the whole open source project code is very clear, logical and code structure is very good, access to their own project is very convenient, within 10 minutes to finish.

First, download the Open source project, import Lib

Project address:Https://github.com/Manabu-GT/ExpandableTextView

This project was written in Android studio, and if you build it with eclipse, you need to make some changes. If you don't bother to change, use the source code I shared at the end of this article.

Second, put the control in the XML

This type of control that can be expanded after a click is called: Expandabletextview, which is also a custom control, which is naturally placed in XML. Just remember to write the namespace:xmlns:expandabletextview= "Http://schemas.android.com/apk/res-auto"

<Com.ms.square.android.expandabletextview.ExpandableTextViewAndroid:id= "@+id/expand_text_view"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical"Expandabletextview:maxcollapsedlines= "8"Expandabletextview:animalphastart= "1">        <TextViewAndroid:id= "@id/expandable_text"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margintop= "8DP"Android:layout_marginleft= "10DP"Android:layout_marginright= "10DP"android:fontfamily= "Sans-serif-light"android:textsize= "16SP"Android:textcolor= "#666666" />        <ImageButtonAndroid:id= "@id/expand_collapse"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margin= "16DP"android:layout_gravity= "Right|bottom"Android:background= "@android: Color/transparent"/>    </Com.ms.square.android.expandabletextview.ExpandableTextView>

Note: If you do not have special needs, please do not delete the TextView and ImageView. Also make sure that the IDs of the two controls are: Expandable_text,expand_collapse.

We can now see that this control is actually a container containing the two view, the preview in the editor is as follows:

If you have an error in your eclipse hint preview, you can't load drawable or something, just reboot. Because it uses the default arrow in Lib (the picture in the lower-right corner)

Third, set the control in XML

Let's take a look at this. Properties that can be set by a custom control

3.1 Expandabletextview

Also, you can optionally set the "following attributes in your layout" XML file to customize the behavior of the Expandablet Extview.

    • maxCollapsedLines(Defaults to 8) The maximum number of text lines allowed to being shown when the TextView gets collapsed

    • animDuration(Defaults to 300MS) Duration of the Animation for the Expansion/collapse

    • animAlphaStart(Defaults to 0.7f) Alpha value of the TextView when the animation starts (NOTE) Set This value to 1 if you want to disable the Alpha Animatio N.

    • expandDrawableCustomize a drawable set to ImageButton to expand the TextView

    • collapseDrawableCustomize a drawable set to ImageButton to collapse the TextView

maxCollapsedLines:TextView the number of rows that can be displayed after the collection, the default value is 8, where 8 is the meaning of 8 rows

animDuration:动画持续时间,设置点击后textView展开的动画时间,默认是300,单位ms。这个不建议修改

animAlphaStart:动画开始的透明度,点击后textview会从这个透明度开始慢慢变到不透明,默认是0.7f。我建议修改为1(不透明),保证不会出现闪烁

Expanddrawable: The ImageView in the lower right corner of the image displayed when TextView expands, is the drawable type

Collapsedrawable: ImageView in the lower right corner of the TextView when the picture is displayed, is the drawable type

Aside: Although this can be set through XML attributes, it does not provide a perfect Java code Setup scheme, so if you really need to be able to customize in the source code to add.

3.2 TextView

The best way to do this open source project is to maximize the reference to the existing controls, where the text is still textview, we can easily operate. The outer expandabletextview is a container that modifies the TextView.

        <TextViewAndroid:id= "@id/expandable_text"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margintop= "8DP"Android:layout_marginleft= "10DP"Android:layout_marginright= "10DP"android:fontfamily= "Sans-serif-light"android:textsize= "16SP"Android:textcolor= "#666666" />

Here can set the text color and so on, as to fontfamily I say, it is set font properties, if not used this property, then will adopt the system default, if the same as above with sans-serif fine font, then the following side of the image is shown

Android:FontFamily="Sans-serif" //Roboto regularAndroid:FontFamily= "sans-serif-light" //Roboto light Android:fontfamily = "sans-serif-condensed" //Roboto condensed Android:fontfamily = "Sans-serif-thin" Android:fontfamily= "Sans-serif-medium" Roboto Medium (Android 5.0)

In combination with

android:textStyle="normal|bold|italic"

This variants is possible:

    • Roboto Regular
    • Roboto Italic
    • Roboto Bold
    • Roboto Bold Italic
    • Roboto-light
    • Roboto-light Italic
    • Roboto-thin
    • Roboto-thin Italic
    • Roboto-condensed
    • roboto-condensed Italic
    • Roboto-condensed Bold
    • Roboto-condensed Bold Italic
    • Roboto-medium
    • Roboto-medium Italic

From:http://stackoverflow.com/questions/12128331/how-to-change-fontfamily-of-textview-in-android/12128448

Iv. using in Java code

Using the same way as TextView, no society can say more. If you are using it in a ListView, just put it in a view and you can refer to the demo provided by the project.

 Packagecom.example.test;ImportCom.ms.square.android.expandabletextview.ExpandableTextView;Importandroid.app.Activity;ImportAndroid.os.Bundle; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Expandabletextview EXPTV=(Expandabletextview) Findviewbyid (R.id.expand_text_view); //exptv.settext ("Hello World");Exptv.settext (getString (R.string.android5_0_text)); }}

Strings.xml

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "App_name">Test</string>    <stringname= "Hello_world">Hello world!</string>    <stringname= "Android5.0_text">Google has tracked the number of system installs in each version, with the Android 4.4 KitKat system rising from 33.9 (per cent) to 39.1 last December, while Android 5.0 did not even appear on the list. It is important to note that Google does not report on a system with a share less than 0.1 (per cent). The android 4.3 Jelly Bean system released two years ago still dominates, with a share of 46 (per cent), Android 4.0 Ice Cream sandwich with a share of 6.7 (per cent), Android 2.3 Gingerb The share of Read is 7.8 (per cent), and the share of Android 2.2 Froyo is 0.4 (per cent). Although many users have not yet used the Android 5.0 system, this does not prevent Google's update does not send.        The news says Google may release Android 5.1 this February, including: The missing mute mode in Android 5.0 is re-added, system stability improves, memory management performance is improved, the application suddenly shuts down, fixed the problem of excessive consumption of network equipment when using Wi-Fi, fixed Wireless network connectivity issues. </string></Resources>

Five, say a few words

Finally want to share the idea of this effect, view in the drawing process will have its own size, and we can add animation to the view to make it change the size of the changes in its size when the adjacent view will be squeezed, This is the reason that the bottom view is pressed after the click TextView expansion appears in the ListView. In the final analysis, our idea of implementation is the way view to the screen, click on the view will appear animation, so that their size really change, in the change to the TextView to draw, so that the present effect. So, a lot of things that look tall, if we analyze carefully, we will find that the principle is actually very simple. We can also achieve, but others have done, we should not repeat the wheel.

SOURCE Download:http://download.csdn.net/detail/shark0017/8346821

 

Create a textview that can be expandabletextview with open source projects

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.