我有一個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" />
謝謝,那工作!我認爲0dp和wrap_content是完全一樣的,因爲eclipse建議我改爲0dp「使用0dip的layout_height而不是wrap_content來獲得更好的性能」 – antoniom 2012-03-05 07:46:56
Eclipse提出了這個建議,因爲您已將'layout_weight'參數應用於視圖。既然你的目標是讓視圖包裹文本內容,我不確定'layout_weight'是否真的有必要。 – Devunwired 2012-03-05 15:52:22