【问题标题】:How to set bigger text into textview with width wrap_content?如何将更大的文本设置为宽度为 wrap_content 的 textview?
【发布时间】:2012-12-19 07:11:13
【问题描述】:

我有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 是我自己的 classgetCalendar() 返回 Calendar 对象,eventsListarraylistEvent 对象。

这里是setTitleDate函数:

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

topDatetextview

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 不起作用。

所以我尝试过这样的变种:

  1. gravity fill 在文本视图中

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

但它没有帮助。

【问题讨论】:

  • 你在设置新文本后尝试过 invalidate() 吗?
  • 使您的布局无效,而不仅仅是文本视图?
  • 我看到两个文本视图被放置在另一个之下。那么你的相对方向是水平的?
  • 相对布局中的方向无关紧要

标签: android textview width settext


【解决方案1】:

在你的文本视图中使用这一行

android:singleLine="true"

【讨论】:

  • 对于第一个 textview 指定 android:layout_width="60dp" 对于第二个 textview 使用 fill_parent。
  • 我认为您可以将 wrap_content 用于第一个文本视图,但第二个需要使用 fill_parent。
  • 请分享你想要的截图
  • 在我的截图上你可以看到两个屏幕。在第二个我设置文本“12 月 20 日星期四”,但它比最初大:“СЕГОДНЯ,12 月 20 日”和“12 月”被剪裁。我需要设置整个日期。
  • 您是否尝试将方向更改为垂直?然后 textview 的 layout_width 到 match_parent?
【解决方案2】:
<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多