When TextView and pictures are displayed in Android, the text ellipsis is displayed, and the picture is automatically on the right.

Source: Internet
Author: User

A very tangled title, really do not know how to describe the phenomenon.

Film Bar, first look at what the demand is.

1. Need:?

The video and ticket icons are displayed after the title, and when the title is too long the icon displays to the ellipsis ... After (the TextView ellipsis is displayed, the icon automatically leans back).

2. Problem solving?

Originally is a very normal demand, looks very simple, but did encounter a very egg pain problem, how to do not, blocked for a long time, the following talk about the solution, encounter such problems can be used by friends.

TextView can omit a single line by android:ellipsize= "End" android:singleline= "true",

But when we have the icon followed by the text, the first time to think of LinearLayout layout_weight to do, but the effect is not the same.

Later unexpectedly forget this attribute: Specify the maximum width of the TextView to automatically omit the android:maxwidth= "90DP", plus this resolves.

But the problem is that the value cannot be written as fixed and adapted to the screen. You can calculate the width of the screen, and then dynamically add the value. The code is as follows:

// to calculate the screen width, dynamic display        New displaymetrics ();        (Activity) mcontext). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);         int width = dm.widthpixels;         -Uiutils.getpixels (Typedvalue.complex_unit_dip, 78));

The PX conversion class used inside is:

 Public Static int getpixels (intfloat  size) {        = resources.getsystem (). Getdisplaymetrics ();         return (int) typedvalue.applydimension (Unit, size, metrics);    }

When TextView and pictures are displayed in Android, the text ellipsis is displayed, and the picture is automatically on the right.

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.