如何在LinearLayout中正确设置多行TextView省略号

时间:2015-06-03 15:18:16

标签: android android-layout textview

我有如下布局层次结构:

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

结果视图是这样的: enter image description here

包装器LinearLayout的高度将动态改变。因此它不固定为63dp。事实是细节正在切割,不能满足我的需要。我想要的是那个最后一条完整的可见行更改为&#34; XXX ...&#34;。就像这样:

enter image description here

从我的代码中,我将动态更改LinearLayout的高度。

LinearLayout wrapper = (LinearLayout)findViewById(R.id.wrapper);

wrapper.getLayoutParams().height = // dynamical height;

2 个答案:

答案 0 :(得分:3)

最好的办法是利用maxLinesellipsize属性。

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

答案 1 :(得分:0)

function groupOthers (obj, n) { var indicesToKeep = _.chain(obj) .map(function (item, index) { //zip each item with its index return {item: item, index: index} }) .sortBy(function (zipped) { //sort by the val (highest to lowest) return -zipped.item.val }) .take(n) //take the top 5 .map('index') // we only care about the indices .value() var partitioned = _.partition(obj, function (item, index) { return _.includes(indicesToKeep, index) }) var finalObjJoined = partitioned[0].concat({ name: 'others', val: _.sum(partitioned[1], 'val') }) return [_.map(finalObjJoined, 'name'), _.map(finalObjJoined, 'val')] } console.log(groupOthers(x, 5)) 更改为android:layout_height="63dp"