问题
I have ListView row implemented as LinearLayout. There are 3 TextViews inside oriented horizontally and aligned to the left. Normally a row looks like this:0 Normal Nickname 18.12.2013
But sometimes the nickname text is very long, so I want it to be ellipsized when all three TextViews don't feet on the screen. I want text to look like this:0 Ridiculously Long Ni... 18.12.2013
but I get this instead:0 Ridiculously Long Nickname 18.12.2
Layout
<TextView
android:id="@+id/cmnt_row_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
/>
<TextView
android:id="@+id/cmnt_row_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cmnt_row_rating"
android:gravity="left"
android:lines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
/>
<TextView
android:id="@+id/cmnt_row_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cmnt_row_author"
android:gravity="left"
android:lines="1"
/>
Is there a way to achieve effect I need?
回答1:
Try this.. Use android:singleLine="true"
for that TextView
and also equally space for each TextView
use android:layout_weight="1"
.For weight
you need to use LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/cmnt_row_rating"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:gravity="left"
/>
<TextView
android:id="@+id/cmnt_row_author"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:lines="1"
android:singleLine="true"
android:ellipsize="end"
android:scrollHorizontally="true"
/>
<TextView
android:id="@+id/cmnt_row_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:gravity="left"
android:lines="1"
/>
</LinearLayout>
回答2:
Set the android:ems=""
like this:
<TextView
android:id="@+id/cmnt_row_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
/>
<TextView
android:id="@+id/cmnt_row_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cmnt_row_rating"
android:gravity="left"
android:lines="1"
android:ellipsize="end"
android:ems="3"
android:scrollHorizontally="true"
/>
<TextView
android:id="@+id/cmnt_row_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cmnt_row_author"
android:gravity="left"
android:lines="1"
/>
I have set it 3. Change it according to your need.
回答3:
use android:maxEms
attribute to limit characters in your TextView
. change maxEms value according to your need.
<TextView
android:id="@+id/cmnt_row_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/cmnt_row_rating"
android:gravity="left"
android:lines="1"
android:ellipsize="end"
android:maxEms="4"
android:scrollHorizontally="true"
/>
来源:https://stackoverflow.com/questions/20650620/ellipsize-with-multiple-text-views