2012-02-03 17 views
46

Trong màn hình ứng dụng của tôi, tôi muốn hiển thị Tiêu đề là trung tâm theo chiều ngang. Tôi đã thử với mã xml dưới đây bố tríTrung tâm văn bản nằm ngang trong RelativeLayout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="10dip" 
    > 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:text="Connections" /> 
</RelativeLayout> 

nhờ

+1

xem http://stackoverflow.com/questions/8767655/application-name-center-alignment-in-android/8767692#8767692 – NagarjunaReddy

+1

< RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_marginTop = "10dip" > Android

Trả lời

114

android:gravity điều khiển sự xuất hiện trong TextView. Vì vậy, nếu bạn có hai dòng văn bản, chúng sẽ được căn giữa trong giới hạn của TextView. Hãy thử android:layout_centerHorizontal="true".

+2

cảm ơn bạn đã giải pháp tốt đẹp – Riskhan

+0

Chào đón nhiều nhất, chúc bạn may mắn. –

+0

Lưu ý rằng điều này sẽ không hoạt động nếu RelativeLayout chứa thuộc tính android: gravity = "center_horizontal". – CoolMind

4

sử dụng layout_centerInParent="true"(layout_centerInHorizontal,layout_centerInVertical)

trọng lực có nghĩa là Algin của văn bản trên TextView, không phải là quan điểm riêng của mình

+0

cảm ơn bạn rất mu ch – Riskhan

15

Sử dụng này:

android:layout_centerHorizontal="true" 

trong TextView

+0

cảm ơn bạn rất nhiều – Riskhan

+0

xem ra cho dấu chấm phẩy không cần thiết ở cuối – Matt

+0

@Matt, tôi đã chỉnh sửa câu trả lời và xóa dấu chấm phẩy không cần thiết. ;) – ViliusK

1

chỉ làm tăng thêm này android:gravity="center_horizontal"

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="10dip" 
    android:gravity="center_horizontal" 
    > 
    <TextView 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:text="Connections" /> 
</RelativeLayout> 
5
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="10dip" 
    > 
    <TextView 
    android:layout_centerHorizontal="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="Connections" /> 
</RelativeLayout> 
+0

hi cảm ơn giải pháp của bạn cũng làm việc – Riskhan

4

thay TextView với mã dưới đây

<TextView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center_horizontal" 
android:text="Connections" /> 
+0

cảm ơn bạn nó đang làm việc, tôi nghĩ rằng sự khác biệt duy nhất là wrap_content để fill_parent – Riskhan

Các vấn đề liên quan