天天看點

Android的TextView中的文字居中顯示

頁面布局是LinearLayout裡包一個TextView控件

1.方法一:TextView設定android:gravity=”center”,同時TextView設定android:layout_width=”match_parent”

舉例代碼:

<LinearLayout android:orientation=”horizontal”    
android:layout_width=”match_parent” android:layout_height=”40sp”    
android:background=”@drawable/top_businesscard_1″>   
<TextView android:layout_width=”match_parent” android:gravity=”center”    
android:textColor=”#447324″ android:textSize=”18dp” android:text=”@string/logo_text”    
android:layout_height=”wrap_content” android:layout_marginTop=”10dp”/>   
</LinearLayout>  
           

方法二:LinearLayout設定android:gravity=”center_horizontal”,同時TextView設定android:layout_width=”wrap_content”

舉例代碼:

<LinearLayout android:orientation=“horizontal”   
android:layout_width=“match_parent” android:layout_height=“40sp”   
android:background=“@drawable/top_businesscard_1″ android:gravity=“center_horizontal”>   
<TextView android:layout_width=“wrap_content”   
android:textColor=“#447324″ android:textSize=“18dp” android:text=“@string/logo_text”   
android:layout_height=“wrap_content” android:layout_marginTop=“10dp”/>   
</LinearLayout>   
           

繼續閱讀