The use of Android Ellipsize and the realization of the results of the marquee summary

Source: Internet
Author: User

Resources:

http://blog.csdn.net/huiwolf2008/article/details/7901084

Http://www.cnblogs.com/Gaojiecai/archive/2013/06/18/3142783.html

In TextView and EditText, you can use Ellipsize to set up text overflow hiding, such as: "A long text ..."

Use the following:

in XML

Android:ellipsize = "End" ellipsis at the end

Android:ellipsize = "Start" ellipsis at the beginning

Android:ellipsize = "Middle" ellipsis in the middle

Android:ellipsize = "Marquee" marquee

It is best to add a constraint Android:singleline = "true" or android:maxlines= "1"

Of course, you can also use code statements

Tv.setellipsize (TextUtils.TruncateAt.valueOf ("END"));

Tv.setellipsize (TextUtils.TruncateAt.valueOf ("START"));

Tv.setellipsize (TextUtils.TruncateAt.valueOf ("Middle"));

Tv.setellipsize (TextUtils.TruncateAt.valueOf ("MARQUEE"));

It is best to add a constraint tv.setsingleline (true);

Note: Marquee mode is not supported in EditText!

Recently inadvertently saw the code related to the effect of the marquee, so in the online access to a lot of information, here to see some of their articles to summarize, by the way, plus some of their own experience.

Let's step down gradually.

First of all we want to achieve merry such an effect, usually is in the TextView this control to achieve, and the text must be a single-line display, if the multiline display, that merry effect

Also lost the meaning of existence. In addition, the use of merry in EditText is not necessary, nor reasonable, in fact, for EditText android:ellipsize This property only for the text set in Android:hint

is useful, and the usage of android:ellipsize= "marquee" cannot be used on EditText controls. Android:ellipsize This property is not useful for text that is entered by edittext users. About EditText

Set the relevant usage of android:ellipsize later, in this case also leave a mark to prevent oneself forget.

Implement our Merry effect in TextView, we need two properties android:singleline= "true", and Android:ellipsize= "marquee", let's take a look at the following code:

    <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout      xmlns:android= "http://schemas.android.com/apk/ Res/android "          android:layout_width=" Fill_parent "          android:layout_height=" Fill_parent "           android:orientation= "vertical" >                <TextView              android:layout_width= "100dip"               android:layout_height= "Wrap_content"              android:layout_gravity= "center"               android:text= "Demo of Merry Effect"               android:singleline= "true"              android:ellipsize = "marquee"/>            </LinearLayout>  

After running this code, we will find that the merry effect is not displayed and the text shown is not moving.

The reason for this is that the marquee effect requires TextView to get the current focus. However, for TextView this control, his default clickable,longclickable,focusable,

Focusableintouchmode the values of these four properties are false, so the marquee effect will not come out, even if you touch textview with your hand or press the navigation button on your phone (now the phone does not have this

A thing. ) is also unable to display the effect of the marquee.

To solve this problem we need to get the focus of our textview, which mainly involves the two properties of android:focusable and Android:focusableintouchmode, simply to set both properties to

True, then run the program after running the Lanterns effect is displayed, here will no longer post these two lines of code.

But after savoring these two attributes, we find that there are still some mystery:

1.. If these two properties are set to Android:focusable= "true" and android:focusableintouchmode= "false", then the merry effect does not appear after the program is run,

This time requires the user to press the phone or the emulator on the top and bottom navigation keys, in order to let the merry effect appears, this shows that android:focusable is for the mobile phone keys valid, however, according to the API explanation,

Android:focusableintouchmode is determined by the screen touch.

2. If these two properties are set to Android:focusable= "false" with android:focusableintouchmode= "true", then merry will not appear anyway, even with Android: Clickable= "true"

Also, this shows that android:focusable= "true" is Android:focusableintouchmode= "true" to be valid prerequisites, which I speculate might be in the source implementation, Android: Focusableintouchmode

The logic is nested in the android:focusable, this waits for further research, the road long its repair far XI ...

3. after both properties are set to true, the merry effect automatically appears after the program runs, which means that the app will automatically follow a certain order (which should be top-down here) after it runs.

Look for the first android:focusableintouchmode= "true" property of the first control, of course, to make this property valid according to the previous discussion android:focusable= "True" must also be available. According to the test,

LinearLayout's Clickable,longclickable,focusable,focusableintouchmode These four properties are also false by default, so in the example above, TextView is the first to get the focus,

Merry also walked up.

Here we do the validation, first modify the code to:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ AndroidAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Vertical"android:focusable= "true" > <TextView android:layout_width= "100dip"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:text= "Demo of Merry Effect"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true" ></TextView> </LinearLayout>

That is, add android:focusable= "true" to LinearLayout and then run the app, and you'll find TextView's merry to go wrong:

Then we add android:focusableintouchmode= "true" to linearlayout and then run it, and we'll find that Merry is dead.

The third conclusion we concluded is also verified here.

But wait a minute, we found the problem here again! Now no matter how we click on the navigation button, or click on the screen TextView Merry dead and alive can not come out. What's going on here?

Let us straighten out the idea, according to our previous summary, merry to be effective, textview must get the focus, now merry not come out, show TextView not get focus.

Here are two things, one is the navigation button can not let TextView or focus, and the second is the screen click can not let TextView get focus.

In the first case, the first case is that using the navigation buttons to toggle the default mode of focus skips the internal controls, so to put it bluntly, the TextView in the above example is inside the linearlayout, and now

LinearLayout has the focus, if you press the navigation button up and down, the focus will only switch between LinearLayout and the same level of control, not into the linearlayout, in order to verify this conclusion, we use

The following code:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ AndroidAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <linearlayout android:layout_width= "fill_parent"Android:layout_height= "100dip"android:focusable= "true"Android:focusableintouchmode= "true"/> <TextView android:layout_width= "100dip"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:text= "Demo of Merry Effect"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true" ></TextView> </LinearLayout>

Then we run the program, we will find that the beginning of the merry did not automatically run, because this time focus on the code inside the second linearlayout there, and then we press the navigation button, we will find merry effect came out,

I don't have stickers here.

But wait, then rerun the application, do not press the navigation button, and then we this time with the finger or the mouse in the simulator to continue to click TextView, will find that merry still does not appear.

This time we came to the second situation.

The problem here can be summed up, in addition to the application of the first time, the application automatically find the Android:focusableintouchmode= "true" property effective control of the ice given focus, how we

Make a control get focus by tapping the screen itself, in which case the control wants to focus on what the process is.

I did not find this information, so I can only do some experiments, and then summarize. There is a suspicion of elephant, but I think it works in some way, go on.

First we'll modify the code to look like this:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Fill_parent"4android:layout_height= "Fill_parent"5android:orientation= "Vertical"6>7<linearlayout android:layout_width= "Fill_parent"8android:layout_height= "100dip"9Android:focusable= "true"TenAndroid:focusableintouchmode= "true" One/> A<TextView -Android:layout_width= "100dip" -android:layout_height= "Wrap_content" theandroid:layout_gravity= "Center" -android:text= "Demonstration of Merry Effect" -Android:singleline= "true" -Android:ellipsize= "Marquee" +Android:clickable= "true" -Android:focusable= "true" +Android:focusableintouchmode= "true" A></TextView> at        -</LinearLayout>

That is, add a android:clickable= "true" property to TextView, and then run to find out, now by touching the way you click TextView can let merry first show that you can get the focus of TextView.

It looks like the problem is solved, but think about some of the things that are worth thinking about:

What is the relationship between Android:clickable and Android:focusableintouchmode? Is there a space that you have to tap if you want to get the focus? Or a space, as long as you can click

Must be able to get the focus? Are these both sufficient and necessary conditions or sufficient or necessary?

Let's do the verification:

First run the following code:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Fill_parent"4android:layout_height= "Fill_parent"5android:orientation= "Vertical"6>7<linearlayout android:layout_width= "Fill_parent"8android:layout_height= "100dip"9Android:clickable= "true"Ten/> One<TextView AAndroid:layout_width= "100dip" -android:layout_height= "Wrap_content" -android:layout_gravity= "Center" theandroid:text= "Demonstration of Merry Effect" -Android:singleline= "true" -Android:ellipsize= "Marquee" -Android:clickable= "true" +Android:focusable= "true" -Android:focusableintouchmode= "true" +></TextView> A        at</LinearLayout>

Run will find that after the application began to run lanterns immediately appear, the mouse click on the textview above the position is the second LinearLayout area, the marquee does not stop, this shows:

Android:clickable= "True" does not necessarily get the focus, which means that a space can be clicked not necessarily to get the focus.

Let's take a look at the code:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Fill_parent"4android:layout_height= "Fill_parent"5android:orientation= "Vertical"6>7<linearlayout android:layout_width= "Fill_parent"8android:layout_height= "100dip"9Android:clickable= "false"TenAndroid:focusable= "true" OneAndroid:focusableintouchmode= "true" A/> -<TextView -Android:layout_width= "100dip" theandroid:layout_height= "Wrap_content" -android:layout_gravity= "Center" -android:text= "Demonstration of Merry Effect" -Android:singleline= "true" +Android:ellipsize= "Marquee" -Android:clickable= "true" +Android:focusable= "true" AAndroid:focusableintouchmode= "true" at></TextView> -        -</LinearLayout>

After this code runs, the second linearlayout in the code first gets the focus automatically, and then we click TextView. The marquee appears, TextView gets the focus, and then we click on the upper TextView area,

The marquee does not stop. This means that if a space is given a touch mode focus but cannot be clicked, it will still not get the focus in touch mode, regardless of how it is touched.

Okay, let's see the last piece of code:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Fill_parent"4android:layout_height= "Fill_parent"5android:orientation= "Vertical"6>7<linearlayout android:layout_width= "Fill_parent"8android:layout_height= "100dip"9Android:clickable= "true"TenAndroid:focusable= "true" OneAndroid:focusableintouchmode= "true" A/> -<TextView -Android:layout_width= "100dip" theandroid:layout_height= "Wrap_content" -android:layout_gravity= "Center" -android:text= "Demonstration of Merry Effect" -Android:singleline= "true" +Android:ellipsize= "Marquee" -Android:clickable= "true" +Android:focusable= "true" AAndroid:focusableintouchmode= "true" at></TextView> -        -</LinearLayout>

After this code runs, first merry does not appear, the second linearlayout in the code gets the focus, then we click the second TextView, Merry appears, and then we click on the area above the TextView,

The merry effect disappears, stating that the focus shifts to the second linearlayout in the code.

Okay, sum up. That is, in touch mode android:clickable= "true" is (android:focusable= "true", android:focusableintouchmode= "true" ) The necessary conditions to gain focus,

This means that a control that wants to focus in touch mode must be clickable, with all three attributes.

The above is the two authors of the attempt, I did not verify, and so I have time to verify their own, but I think the article is good, or first save it!

The use of Android Ellipsize and the realization of the results of the marquee summary

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.