Android TextView with multiline and “android:ellipsize = middle”

后端 未结 5 1471
我寻月下人不归
我寻月下人不归 2020-12-21 04:25


        
相关标签:
5条回答
  • 2020-12-21 05:07

    Try the below code, its working :

    <TextView
            android:id="@+id/food_item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom|center_horizontal"
            android:maxLines="1"
            android:ellipsize="middle"
            android:scrollHorizontally="true"
            android:textSize="17sp"
            android:textColor="@color/food_cell_text"
            android:text=""Cured beef salad with rocket lettuce,cherry tomato & grana padano""/>
    
    0 讨论(0)
  • 2020-12-21 05:12

    android:ellipsize="middle" doesn't seem to work with multiple lines on Android 4.0 and later. you could try the following which will work, however you only have the one line. Add the following to your textview.

    android:singleLine="true"
    
    0 讨论(0)
  • 2020-12-21 05:14

    If setMaxLines(int) has been used to set two or more lines, only TextUtils.TruncateAt.END and TextUtils.TruncateAt.MARQUEE are supported (other ellipsizing types will not do anything). https://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize

    if you want to use textview with 2 lines and ellipsize=middle you can divide your text in half and to do use it

          <TextView
                android:id="@+id/food_item_name_first_line"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="bottom|center_horizontal"
                android:singleLine="true"
                android:ellipsize="end"
                android:textSize="17sp"
                android:text="Cured beef salad with rocket "/>
           <TextView
                android:id="@+id/food_item_name_second_line"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="bottom|center_horizontal"
                android:singleLine="true"
                android:ellipsize="start"
                android:textSize="17sp"
                android:text="lettuce,cherry tomato & grana padano"/> 
    

    it looks like this

    0 讨论(0)
  • 2020-12-21 05:16

    Try this,

         <TextView
            android:id="@+id/food_item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:ellipsize="middle"
            android:layout_margin="50dp"
            android:padding="5dp"
            android:gravity="center"
            android:maxLines="2"
            android:textColor="@color/keyBagroung"
            android:text="@string/Curved"/>
    

    Try to use string in Strings.xml file, Like this

     <string name="Curved">Cured beef salad with rocket lettuce,cherry tomato &amp; grana padano</string>
    

    because special characters are not allowed in xml file.

    0 讨论(0)
  • 2020-12-21 05:18

    try this code:

      <TextView
        android:id="@+id/food_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txt"
        android:layout_below="@+id/txt"
        android:layout_marginTop="64dp"
        android:ellipsize="middle"
        android:gravity="bottom|center_horizontal"
        android:maxLines="2"
        android:text="Cured beef salad with rocket lettuce,cherry tomato , grana padano"
        android:textColor="#000000"
        android:textSize="17sp" />
    

    this is screenshot

    0 讨论(0)
提交回复
热议问题