How to show two Textview with ellipsize end in horizontal Linearlayout in android?

橙三吉。 提交于 2020-06-11 06:47:51

问题


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.

Below Images.

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

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


回答1:


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

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

Refer this.

<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>

EDIT 1:

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>

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




回答2:


use weight sum for your ellipsize text view so it will always give space for second textview.

<LinearLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content">

            <TextView android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:singleLine="true"
                android:ellipsize="end"
                android:text="this is for checking that ellipsize is working or not in the view"/>

            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="20"
               />

        </LinearLayout>




回答3:


<?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="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="10"
    android:padding="10dp">

        <TextView
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:paddingRight="5dp"
        android:singleLine="true"
        android:maxLength="25"
        android:textColor="@color/dark_gray"
        android:text="rtert"
        android:textSize="@dimen/common_text_label" />

        <TextView
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:gravity="right"
        android:text="rtyrty"
        android:textColor="@color/dark_gray"
        android:textSize="@dimen/common_text_label"/>



</LinearLayout>



回答4:


i just added only two TextView according to your design set

Try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/img_timeline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginLeft="15dp"
            android:src="@drawable/ic_launcher"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="13dp"
            android:orientation="vertical"
            android:weightSum="1">

            <TextView
                android:id="@+id/text_1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:text="ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceSmall"
                />

            <TextView
                android:id="@+id/text_2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:text="This is Demo for Just Testing and Its only Demo for Testing."
                android:textSize="10dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Hope it may helps.




回答5:


Here is the solution to your problem, just copy and past with you,

         <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"
        android:weightSum="100" >

        <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:layout_weight="70"
            android:src="@mipmap/ic_launcher" />

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

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

                <RelativeLayout
                    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_alignParentLeft="true"
                        android:layout_toLeftOf="@+id/editText"
                        android:ellipsize="end"
                        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_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="3dp"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:layout_alignParentRight="true"
                        android:text="(20)"
                        android:textSize="12sp" />
                </RelativeLayout>

                <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:layout_weight="10"
            android:src="@android:drawable/ic_delete" />
    </LinearLayout>
    </android.support.v7.widget.CardView> 

</RelativeLayout>

if this solve your problem please mark the answer as correct.



来源:https://stackoverflow.com/questions/37389118/how-to-show-two-textview-with-ellipsize-end-in-horizontal-linearlayout-in-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!