简体   繁体   中英

How to achieve this layout in android?

Please see below, I think this should be simple but I cannot figure it out...

The goal is to make an image follows some text immediately, and when the text is long, the image can still be there and the text show ellipsis at the end. The text is only one line.

Thanks!

在此处输入图片说明

Something lke this should work:

<TextView   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ABCABCABACBACBACBACBAC"
        android:maxLines="1"
        android:singleLine="true"
        android:ellipsize="end"    
        android:drawableRight ="@drawable/ic_info" />

try this android:ellipsize="end"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableEnd="@drawable/images"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Large Text fgkjdsfgkjdfslgk jdsfg jdfgjdfjgijdfogjdfgdfsgdfsg dfsg  dfsg sdf g"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableEnd="@drawable/images"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

In above comments you have asked for listener to click event on drawableRight, here is the implementation to that

yourTextView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_RIGHT = 2;

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (event.getRawX() >= (yourTextView.getRight() - yourTextView.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

                        return true;
                    }

                }
                return false;
                }

        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM