Android 如何将更大的文本设置为具有宽度换行内容的文本视图?
我有Android 如何将更大的文本设置为具有宽度换行内容的文本视图?,android,textview,width,settext,Android,Textview,Width,Settext,我有列表视图。它的每个元素都有date(Calendar对象),所以我想在顶部可见元素中显示当前日期。我已经实现了ListView.OnScrollListener public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int index = firstVisibleItem; if (index <
列表视图
。它的每个元素都有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());
}
}
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\u top\u date textview
具有宽度wrap\u content
,因此当我在setTitleDate
中动态设置文本时,如果新文本的width
较大,则textview的宽度不会变大,文本会被剪裁。
我不知道为什么包装内容不起作用
因此,我尝试了这样一种变体:
重力填充
在文本视图中
topDate.setLayoutParams(新)
LayoutParams(LayoutParams.WRAP_内容,
LayoutParams.WRAP_内容)代码>
但是它没有帮助。在文本视图中使用这一行
android:singleLine=“true”在文本视图中使用这一行
android:singleLine=“true”
在设置新文本后是否尝试了invalidate()?在布局上无效,而不仅仅是文本视图?我看到两个文本视图一个放在另一个下面。那么你的相对方向是水平的?这与相对布局中的哪个方向无关设置新文本后你是否尝试了invalidate()。那么您的相对方向是水平的?对于第一个文本视图,相对布局中的哪个方向无关紧要,请指定android:layout_width=“60dp”对于第二个文本视图,请使用“填充父视图”。我认为第一个文本视图可以使用“换行”内容,但第二个文本视图需要使用“填充父视图”。请在我的屏幕截图上准确地共享您想要的屏幕截图,您可以看到两个屏幕。在第二段中,我设置了文本“12月20日,星期四”,但它比最初更大:“12月20日,星期四”和“12月”被剪掉。我需要设定整个日期。你试过把方向改成垂直方向吗?然后布局文本视图的宽度以匹配父视图?对于第一个文本视图,指定android:layout\u width=“60dp”对于第二个文本视图,使用fill\u parent。我认为第一个文本视图可以使用wrap\u内容,但第二个文本视图需要使用fill\u parent。请共享您想要的屏幕截图。在我的屏幕截图中,您可以看到两个屏幕。在第二段中,我设置了文本“12月20日,星期四”,但它比最初更大:“12月20日,星期四”和“12月”被剪掉。我需要设定整个日期。你试过把方向改成垂直方向吗?然后布局文本视图的宽度以匹配父视图?
<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>
<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>