2012-03-02 11 views
2

У меня есть TextView, который я раздуваю в своей деятельности. Когда я устанавливаю текст в TextView, его содержимое распространяется по нескольким строкам, так как это поведение по умолчанию. TextView идет singleLine при настройке текста на runOnUiThread

например
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"); 
     .... 
    } 

, когда я использую один и тот же текст внутри Runnable в runOnUiThread текст выходит одну строку по умолчанию. Я должен позвонить setSingleLine (лжи), чтобы быть многострочными ...

например
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"); 
      } 
      }); 
      }); 
    } 

Почему это происходит?

EDIT Мой файл макета, как это:

<?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" /> 

ответ

0

Скорее всего, это связано с ограничениями макета. Причина, по которой вы видите разницу, состоит в том, что в одном случае (вызываемый непосредственно из onCreate()) текст устанавливается до, а выходы выложены, поэтому высота TextView установлена ​​в соответствии с содержимым внутри. Во втором случае (из Thread) текст устанавливается после завершения, а TextView не корректирует его размер, чтобы соответствовать новому контенту, если его LayoutParams явно не говорит об этом.

Если вы установили атрибут layout_height вашего descriptionTextView на номер wrap_content, проблема с изменением размера исчезнет.

HTH

+0

спасибо, что сработало! Я думал, что 0dp и wrap_content были точно такими же, поскольку eclipse посоветовал мне перейти на 0dp «Использовать layout_height 0dip вместо wrap_content для лучшей производительности» – antoniom

+1

Eclipse сделал это предложение, потому что у вас был параметр «layout_weight», примененный к представлению. Поскольку ваша цель состоит в том, чтобы просмотреть содержимое текста, я не уверен, действительно ли 'layout_weight' действительно необходим. – Devunwired

0

Если вы вставили весь макет, мы могли бы попытаться сделать некоторые отладки. TextView.setText() завершает уведомление родительского контейнера и, по-видимому, это ведет себя по-разному в зависимости от того, когда он вызывается.

BTW, глядя на ваш код я думаю, вы могли бы использовать AsyncTask вместо наружной резьбы

+0

Я отредактировал свой вопрос и включил макет. Надеюсь, что вы что-то увидите. – antoniom

Смежные вопросы