【发布时间】:2012-12-19 07:11:13
【问题描述】:
我有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,getCalendar() 返回 Calendar 对象,eventsList 是 arraylist 的 Event 对象。
这里是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));
但它没有帮助。
【问题讨论】:
-
你在设置新文本后尝试过 invalidate() 吗?
-
使您的布局无效,而不仅仅是文本视图?
-
我看到两个文本视图被放置在另一个之下。那么你的相对方向是水平的?
-
相对布局中的方向无关紧要
标签: android textview width settext