如何在android中的水平Linearlayout中显示两个椭圆形末端的Textview? [英] How to show two Textview with ellipsize end in horizontal Linearlayout in android?

查看:87
本文介绍了如何在android中的水平Linearlayout中显示两个椭圆形末端的Textview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的一个问题是,我在一个水平Linearlayout中有两个Textview,在椭圆形末端有一个Textview,当我输入更多文本内容时,它在Textview结尾中显示"...",但是那时我无法显示第二个TextView.

I am facing one problem which is, I have two Textview in one horizontal Linearlayout and one Textview with ellipsize end and When I have type text more content then it display "..." in Textview ending, But at that time I can not show the second TextView.

在图像下方.

但我都想同时使用Textview.如果第一个Textview"具有更多内容,则显示椭圆形"结尾,并显示第二个"Textview".

But I want to both Textview. If First Textview is with more content then it display ellipsize end and also display second Textview.

在图像下方. 我的Xml布局是

Below Image. My Xml Layout is,

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

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        card_view:cardBackgroundColor="#ffffff"
        card_view:cardUseCompatPadding="true"
        card_view:contentPadding="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv_profile_photo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="center"
                android:src="@mipmap/ic_launcher" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/tv_name"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:ellipsize="end"
                            android:singleLine="true"
                            android:text="ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC"
                            android:textSize="20sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/editText"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="3dp"
                            android:layout_toRightOf="@+id/tv_name"
                            android:layout_weight="1"
                            android:singleLine="true"
                            android:text="(20)"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:gravity="end"
                        android:singleLine="true"
                        android:text="Yesterday"
                        android:textSize="12sp" />
                </LinearLayout>

                <TextView
                    android:id="@+id/desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="3dp"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="This is Demo for Just Testing and Its only Demo for Testing."
                    android:textSize="14sp" />

            </LinearLayout>

            <ImageView
                android:id="@+id/iv_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/ic_next" />

        </LinearLayout>
    </android.support.v7.widget.CardView>


</RelativeLayout>

请分享我的想法. 谢谢

Please share me idea. Thanks

推荐答案

给第一个孩子Text View而不是第二个孩子.您无需为此使用任何固定宽度.

Give a Weight to your First Child Text View not Second. You not need to use any Fixed width with this.

只需将android:layout_weight="1"分配给第一个孩子Text View.

Just give android:layout_weight="1" to First child Text View only.

请参阅.

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:ellipsize="end"
            android:id="@+id/tv_name"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:singleLine="true"
            android:text="ABC ABC ABC ABC ABC ABC...(20)"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/editText"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/tv_name"
            android:layout_width="wrap_content"
            android:singleLine="true"
            android:text="(20)"
            android:textSize="12sp" />

    </LinearLayout>

我也使其适用于少量内容.我已经删除了嵌套Linear Layouts,这对性能不利.我已对您的Layout进行了更改,请替换为您的XML.

I have made it work for small content also. I have removed Nested Linear Layouts which is bad for performance. I have applied changes to your Layout please replace this with your XML.

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

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        card_view:cardBackgroundColor="#ffffff"
        card_view:cardUseCompatPadding="true"
        card_view:contentPadding="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv_profile_photo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="center"
                android:src="@mipmap/ic_launcher" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="0.90">

                    <TextView
                        android:id="@+id/tv_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_weight="0.90"
                        android:ellipsize="end"
                        android:singleLine="true"
                        android:text="ABCABC ABC ABC ABC ABC ABC...(20)"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/editText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="3dp"
                        android:layout_toRightOf="@+id/tv_name"
                        android:singleLine="true"
                        android:text="(20)"
                        android:textSize="12sp" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:gravity="end"
                        android:singleLine="true"
                        android:text="Yesterday"
                        android:textSize="12sp" />
                </LinearLayout>

                <TextView
                    android:id="@+id/desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="3dp"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="This is Demo for Just Testing and Its only Demo for Testing."
                    android:textSize="14sp" />

            </LinearLayout>

            <ImageView
                android:id="@+id/iv_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/ic_launcher" />

        </LinearLayout>
    </android.support.v7.widget.CardView>


</RelativeLayout>

请不要使用Nested Linear Layouts,因为它会降低性能.

Never use Nested Linear Layouts because it is bad for Performance.

这篇关于如何在android中的水平Linearlayout中显示两个椭圆形末端的Textview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆