Android 在runOnUiThread上设置文本时,文本视图将变为单线
我有一个在活动中膨胀的文本视图。当我将文本设置为该文本视图时,它的内容会分散在多行中,因为这是默认行为。 e、 g 当我在RunUnuithRead中的Runnable中使用相同的文本时,默认情况下文本是单行的。我必须调用setSingleLine(false)才能进行多行。。。 e、 g 为什么会这样 编辑 我的布局文件如下所示:Android 在runOnUiThread上设置文本时,文本视图将变为单线,android,textview,runnable,Android,Textview,Runnable,我有一个在活动中膨胀的文本视图。当我将文本设置为该文本视图时,它的内容会分散在多行中,因为这是默认行为。 e、 g 当我在RunUnuithRead中的Runnable中使用相同的文本时,默认情况下文本是单行的。我必须调用setSingleLine(false)才能进行多行。。。 e、 g 为什么会这样 编辑 我的布局文件如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:
<?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()
调用),文本在布局视图之前设置为,因此TextView
高度设置为与内部内容匹配。在第二种情况下(从线程
),文本在布局完成后设置,并且文本视图
不会调整其大小以匹配新内容,除非其布局参数
明确告诉它这样做
如果将descriptionTextView
的layout\u height
属性设置为wrap\u content
,则大小调整问题将消失
HTH我已经编辑了我的问题,并包括了布局。希望你能看到一些东西。谢谢,这很有效!我认为0dp和wrap\u内容是完全相同的,因为eclipse建议我更改为0dp“使用0dip的布局高度而不是wrap\u内容以获得更好的性能”。eclipse提出了这一建议,因为您在视图中应用了
布局权重
参数。因为您的目标是让视图包装文本内容,所以我不确定是否真的需要layout\u weight
。
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" />