Android TextView and picture peer display (text beyond ellipsis, picture automatically to the right) _android

Source: Internet
Author: User

First, the demand:

The title may not write enough, the next look at the picture, we will understand what is meant. The video and ticket icon is followed by the title, and when the title is too long, icon displays to the ellipsis ... After (the TextView ellipsis is displayed, the icon automatically leans back).

Ii. Problem-solving

TextView can be android:ellipsize="end" android:singleLine="true" omitted by implementing a single line,

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

Later forgot this attribute: The specified TextView maximum width is automatically omitted android:maxWidth="90dp" , plus this is resolved.

But the problem is, this value cannot be written in a fixed order and should be adapted to the screen. You can calculate the width of the screen, and then add the values dynamically.

The code is as follows:

To calculate the screen width, dynamic display
  displaymetrics DM = new Displaymetrics ();
  (activity) mcontext). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
  int width = dm.widthpixels;
  Holder.tv_postbar_item_title.setMaxWidth (Width-uiutils.getpixels (Typedvalue.complex_unit_dip, 78));

The PX conversion classes used inside are:

public static int getpixels (int unit, float size) {
  Displaymetrics metrics = Resources.getsystem (). getdisplaymetrics ();
  return (int) typedvalue.applydimension (unit, size, metrics);
 

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.