gpt4 book ai didi

android - 在 runOnUiThread 上设置文本时,TextView 变为单行

转载 作者:行者123 更新时间:2023-11-29 02:06:53 26 4
gpt4 key购买 nike

我有一个在 Activity 中膨胀的 TextView。当我将文本设置为此 TextView 时,其内容分布在多行中,因为这是默认行为。例如

    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.location_details);

// inflate the view
descriptionTextView = (TextView)findViewById(R.id.descriptionTextView );
descriptionTextView.setText("This is a long text that should be multilined");
....
}

当我在 runOnUiThread 的 Runnable 中使用相同的文本时,文本默认为单行。我必须调用 setSingleLine(false) 才能成为多行...例如

    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.location_details);

// inflate the view
descriptionTextView = (TextView)findViewById(R.id.descriptionTextView );

new Thread(new Runnable(){
public void run() {
...
runOnUiThread(new Runnable(){
@Override
public void run() {
descriptionTextView.setText("This is a long text that should be multilined");
}
});
});
}

为什么会这样?

编辑我的布局文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="6dp"
android:gravity="center|top"
android:background="@drawable/background">

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@drawable/bg_dashboard"
android:gravity="center|top"
android:orientation="vertical"
android:padding="10dp" >

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_gravity="center"
android:layout_weight="1">

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="top"
android:orientation="vertical" >

<TextView
android:id="@+id/titleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textStyle="bold"
android:textColor="#565A5D"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textSize="24sp" />

<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/divider"
android:contentDescription="@string/metaDivider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingBottom="4dp"
android:paddingTop="4dp" />

<ImageView
android:id="@+id/photoImageView"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:layout_height="160dp"
android:padding="5dp"
android:src="@null"
android:contentDescription="@string/metaArticleImage" />

<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageDivider"
android:src="@drawable/divider"
android:contentDescription="@string/metaDivider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:paddingBottom="4dp"
android:paddingTop="4dp" />

<TextView
android:id="@+id/descriptionTextView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:textColor="#55595C"
android:layout_weight="1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"/>

<Button
android:id="@+id/mapButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mapBtn"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="center" />

</LinearLayout>

</ScrollView>

</LinearLayout>

<ImageView
android:contentDescription="@string/metaLogo"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo" />

最佳答案

这很可能与您的布局限制有关。您看到差异的原因是因为在一种情况下(直接从 onCreate() 调用)文本是在 布局 View 之前设置的,因此 TextView 高度设置为匹配里面的内容。在第二种情况下(来自 Thread),文本是在 布局完成后设置的,TextView 不会调整其大小以匹配新的内容,除非它的 LayoutParams 明确告诉它这样做。

如果将 descriptionTextViewlayout_height 属性设置为 wrap_content,调整大小的问题就会消失。

HTH

关于android - 在 runOnUiThread 上设置文本时,TextView 变为单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9536142/

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