[英]Text being truncated in TextView with width set to wrap_content
[英]How to set bigger text into textview with width wrap_content?
我有listview
。 每个元素都有date
( Calendar
对象),所以我想在顶部可见元素处显示当前日期。 我已经implemented ListView.OnScrollListener
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int index = firstVisibleItem;
if (index < eventsList.size() && eventsList.size() > 0){
Event ev = eventsList.get(index);
setTitleDate(ev.getCalendar());
}
}
Event
是我自己的class
, eventsList
getCalendar()
返回Calendar
对象, eventsList
是Event
对象的arraylist
。
这是setTitleDate
函数:
protected void setTitleDate(Calendar cc){
dateFormatTopDateWithWeekday = new SimpleDateFormat("EEEE, d MMMM");
topDate.setText(dateFormatTopDateWithWeekday.format( cc.getTime() ).toUpperCase());
}
topDate
是一个textview
:
topDate = (TextView)findViewById(R.id.event_top_date);
还有我的模板的一部分:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/cat_top_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:text="@string/main_top_name"
android:textColor="@color/header_title"
android:textSize="17sp"
android:includeFontPadding="false"
android:maxLines="1"
android:typeface="normal" />
<TextView
android:id="@+id/event_top_date"
android:layout_width="wrap_content"
android:background="@android:color/black"
android:layout_height="wrap_content"
android:layout_below="@id/cat_top_name"
android:text="@string/main_top_name"
android:textColor="@color/header_title"
android:textSize="13sp"
android:includeFontPadding="false"
android:maxLines="1"
android:typeface="normal"
/>
</RelativeLayout>
event_top_date textview
的宽度为wrap_content
,所以当我在动态设置文本setTitleDate
如果width
新文本较大,TextView的宽度不会做大文本进行裁剪。 我不知道为什么wrap_content
不起作用。
所以我尝试了这样的变体:
gravity fill
文本视图
topDate.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
但这没有帮助。
在您的textview中使用这一行
android:singleLine =“ true”
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/cat_top_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="main_top_name"
android:textSize="17sp"
android:typeface="normal" />
<TextView
android:id="@+id/event_top_date"
android:layout_width="fill_parent"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_below="@id/cat_top_name"
android:background="@android:color/black"
android:includeFontPadding="false"
android:maxLines="1"
android:text="THURSDAY, 20 DECEMBER"
android:textColor="#ffffff"
android:textSize="13sp"
android:typeface="normal" />
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.