我有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是对象。arraylistEvent
这是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有 width wrap_content,所以当我动态设置文本时setTitleDate,如果width新文本更大,textview 的宽度不会变大并且文本被剪裁。我不知道为什么wrap_content不起作用。
所以我尝试了这样的变体:
gravity fill在文本视图中topDate.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
但这没有帮助。
