gpt4 book ai didi

android - 如何将更大的文本设置为宽度为 wrap_content 的 textview?

转载 作者:行者123 更新时间:2023-11-30 03:52:15 27 4
gpt4 key购买 nike

我有 ListView 。它的每个元素都有 date(Calendar 对象),所以我想在顶部可见元素显示当前日期。我已经实现了 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对象,eventsListEvent 对象的 arraylist

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

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

  1. 重力填充在 TextView 中

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

但这并没有帮助。

最佳答案

在你的 TextView 中使用这一行

android:singleLine="true"

关于android - 如何将更大的文本设置为宽度为 wrap_content 的 textview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13947247/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com