1

我有listview。每个它的元素都有dateCalendar对象),所以我想在顶部可见元素显示当前日期。我有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()对象CalendareventsList是对象。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不起作用。

所以我尝试了这样的变体:

  1. gravity fill在文本视图中

  2. topDate.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

但这没有帮助。

4

2 回答 2

1

use this line in your textview

android:singleLine="true"

于 2012-12-19T07:17:56.137 回答
0
<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>
于 2012-12-19T08:07:18.890 回答