我正在使用支持库中的GridLayout。一些单元格包含播放器信息 - 我使用带有两个TextView的线性布局。我无法获得要包装的名称(第二个文本视图)。
当前行为:
123
John Cochtousczyon
期望的行为:
123
John
Cochtousczyon
这是单元格布局xml的当前状态:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/player_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="test" />
<TextView
android:id="@+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:minLines="2"
android:scrollHorizontally="false"
android:text="testing thisout with extra sauce"
android:textSize="18sp" />
</LinearLayout>
以及将他添加到网格中的代码:
lp = (GridLayout.LayoutParams) playerCell.getLayoutParams();
lp.columnSpec = GridLayout.spec(nameCol);
lp.rowSpec = GridLayout.spec(nameRow);
playerGrid.addView(playerCell, lp);
理想情况下,我想要一个解决方案,在包装名称后设置最宽单元格的列宽。我已经使用maxEms接近了所需的结果,但是这需要平衡最可能需要的限制与更长名称可以容忍的截断量。
P'r'aps没有什么可以解决的问题,只是为了确定宽度 - 只是想我会问,以防所有比我更聪明的人解决了这个问题。
答案 0 :(得分:1)
这是我现在已经解决的问题,直到有更好的方法出现:
<TextView
android:id="@+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="4"
android:maxLines="2"
android:text="testing thisout with extra sauce"
android:textSize="18sp" />
fill_parent,ellipsize END,maxEms和maxLines的组合让我足够接近。
答案 1 :(得分:0)
希望这会对某人有所帮助; 我只是用了好老的&#34; \ n&#34;包装。
答案 2 :(得分:0)
如果有人在2020年仍在寻找它,那么Android现在有一种内置的方法。只需将此代码添加到您的Java文件中
TextviewCompat.setAutoSizeTypeWithDefaults(<your textview refernce here>, TextviewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
还要确保将其添加到textview xml
android:ellipsize="none"