如何在 LinearLayout 中正确设置多行 TextView Ellipsis
How can I set multiline TextView Ellipsis properly in LinearLayout
我的布局层次结构如下:
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="50dp"
android:layout_height="63dp"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:background="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
/>
<TextView
android:id="@+id/detail"
android:background="#0f0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="afasdfasdfasfasdfasfdasfasdfasfasdfsadfasfasdfasdfasdf"
/>
</LinearLayout>
结果视图是这样的:
包装器 LinearLayout 的高度将是动态的 changed.So 它不是固定的 63dp.The 事实上,细节正在被削减,这不能满足我 need.What 我想要的就是把最后一个完整的可见行改成了"XXX..."。像这样:
根据我的代码,我将动态更改 LinearLayout 的高度。
LinearLayout wrapper = (LinearLayout)findViewById(R.id.wrapper);
wrapper.getLayoutParams().height = // dynamical height;
将android:layout_height="63dp"
更改为android:layout_height="wrap_content"
最好的办法是利用 maxLines
和 ellipsize
属性。
<TextView
android:id="@+id/detail"
android:background="#0f0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:text="afasdfasdfasfasdfasfdasfasdfasfasdfsadfasfasdfasdfasdf"
/>
我的布局层次结构如下:
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="50dp"
android:layout_height="63dp"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:background="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
/>
<TextView
android:id="@+id/detail"
android:background="#0f0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="afasdfasdfasfasdfasfdasfasdfasfasdfsadfasfasdfasdfasdf"
/>
</LinearLayout>
结果视图是这样的:
包装器 LinearLayout 的高度将是动态的 changed.So 它不是固定的 63dp.The 事实上,细节正在被削减,这不能满足我 need.What 我想要的就是把最后一个完整的可见行改成了"XXX..."。像这样:
根据我的代码,我将动态更改 LinearLayout 的高度。
LinearLayout wrapper = (LinearLayout)findViewById(R.id.wrapper);
wrapper.getLayoutParams().height = // dynamical height;
将android:layout_height="63dp"
更改为android:layout_height="wrap_content"
最好的办法是利用 maxLines
和 ellipsize
属性。
<TextView
android:id="@+id/detail"
android:background="#0f0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:text="afasdfasdfasfasdfasfdasfasdfasfasdfsadfasfasdfasdfasdf"
/>