简体   繁体   中英

How to set bigger text into textview with width wrap_content?

I have listview . Each it's element has date ( Calendar object), so I want to display current date at top visible element. I have 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 is my own class , getCalendar() returns Calendar object, eventsList is arraylist of Event objects.

Here is setTitleDate function:

protected void setTitleDate(Calendar cc){
    dateFormatTopDateWithWeekday = new SimpleDateFormat("EEEE, d MMMM");
    topDate.setText(dateFormatTopDateWithWeekday.format( cc.getTime() ).toUpperCase());
}

topDate is a textview :

topDate = (TextView)findViewById(R.id.event_top_date);

And a part of my template:

<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 has width wrap_content , so when I set text dynamically in setTitleDate and if width of new text is bigger, the width of textview does not become bigger and text is clipped. I don't know why wrap_content does not work.

So I have tried such a variants:

  1. gravity fill in text view

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

but it didn't help.

use this line in your 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM