Android開發(fā)中,在LinearLayout中如何讓左邊TextView顯示省略號,而右邊的空間正常顯示,完美解決方案:EllipsizeLayout

在開發(fā)中會遇到一個比較變態(tài)但又合理的需求:
在LinearLayout里放入如下控件

    <LinearLayout >
        <TextView />
        <ImageView />
    </LinearLayout>

要求:
1. TextView的寬度足夠短的情形,則ImageView緊隨其后


情形一.png

2. TextView的寬度足夠長的情形,ImageView居右顯示,TextView省略


情形二.png

解決方案一:布局設(shè)置rtf,但必須在Android 4.2以上才有效,前提是不需要適配中東阿拉伯語的應用。
最近又發(fā)現(xiàn)了一種完美解決方案,如何完美解決呢:在系統(tǒng)的源碼里發(fā)現(xiàn)一個繼承與LinearLayout的布局EllipsizeLayout,直接上源碼:
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * Created by zhanghehe on 2018/9/15.
 * desc:
 */
public class EllipsizeLayout extends LinearLayout{
    public EllipsizeLayout(Context context) {
        this(context, null);
    }

    public EllipsizeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (getOrientation() == HORIZONTAL
                && (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY)) {
            int totalLength = 0;
            boolean outOfSpec = false;
            TextView ellipView = null;
            final int count = getChildCount();

            for (int ii = 0; ii < count && !outOfSpec; ++ii) {
                final View child = getChildAt(ii);
                if (child != null && child.getVisibility() != GONE) {
                    if (child instanceof TextView) {
                        final TextView tv = (TextView) child;
                        if (tv.getEllipsize() != null) {
                            if (ellipView == null) {
                                ellipView = tv;
                                // clear maxWidth on mEllipView before measure
                                ellipView.setMaxWidth(Integer.MAX_VALUE);
                            } else {
                                // TODO: support multiple android:ellipsize
                                outOfSpec = true;
                            }
                        }
                    }
                    final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child
                            .getLayoutParams();
                    outOfSpec |= (lp.weight > 0f);
                    measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                    totalLength += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
                }
            }
            outOfSpec |= (ellipView == null) || (totalLength == 0);
            final int parentWidth = MeasureSpec.getSize(widthMeasureSpec);

            if (!outOfSpec && totalLength > parentWidth) {
                int maxWidth = ellipView.getMeasuredWidth() - (totalLength - parentWidth);
                // TODO: Respect android:minWidth (easy with @TargetApi(16))
                int minWidth = 0;
                if (maxWidth < minWidth) {
                    maxWidth = minWidth;
                }
                ellipView.setMaxWidth(maxWidth);
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

布局如下:

        <com.a.b.widgets.EllipsizeLayout
            android:layout_width="@dimen/dp_120"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dp_10"
            app:layout_constraintBottom_toBottomOf="@+id/aivGroupIcon"
            app:layout_constraintStart_toEndOf="@+id/aivGroupIcon"
            app:layout_constraintTop_toTopOf="@+id/aivGroupIcon">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/atvUnionGroupName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="左邊占位"
                android:textColor="@color/colorWhite"
                android:textSize="@dimen/sp_13"
                android:textStyle="bold" />

            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:fontFamily="@font/pingfang_medium"
                android:singleLine="true"
                android:text="占位"
                android:textColor="@color/colorWhite"
                android:textSize="@dimen/sp_13"
                android:textStyle="bold" />

        </com.a.b.widgets.EllipsizeLayout>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容