2012-03-02 33 views
2

我有一個TextView,在我的活動中充氣。當我將文本設置爲此TextView時,其內容分散在多行中,因爲這是默認行爲。 e.g當在runOnUiThread上設置文本時,TextView變爲singleLine

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(假)被multilined ... e.g

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

回答

0

最有可能與您的佈局約束的事情。您看到差異的原因是因爲在一種情況下(直接從onCreate()中調用),文本被設置爲之前的視圖被佈置,所以TextView高度被設置爲匹配裏面的內容。在第二種情況下(來自Thread),在版式完成後,文本被設置爲,並且TextView不會調整其大小以匹配新內容,除非其LayoutParams明確指出這樣做。

如果將descriptionTextViewlayout_height屬性設置爲wrap_content,調整大小問題將消失。

HTH

+0

謝謝,那工作!我認爲0dp和wrap_content是完全一樣的,因爲eclipse建議我改爲0dp「使用0dip的layout_height而不是wrap_content來獲得更好的性能」 – antoniom 2012-03-05 07:46:56

+1

Eclipse提出了這個建議,因爲您已將'layout_weight'參數應用於視圖。既然你的目標是讓視圖包裹文本內容,我不確定'layout_weight'是否真的有必要。 – Devunwired 2012-03-05 15:52:22

0

如果您粘貼整個佈局,我們可以試着做一些更多的調試。 TextView.setText()最終通知父容器,並且顯然這取決於何時調用它的行爲有所不同。

BTW,通過看你的代碼,我想你可以使用一個AsyncTask代替外螺紋

+0

我已編輯我的問題,幷包括佈局。希望你能看到一些東西。 – antoniom 2012-03-02 19:43:41

相關問題