2013-01-01 80 views
38

Tôi có văn bản arabic, do đó tôi đặt trọng lực sang phải để bắt đầu văn bản từ bên phải. Văn bản bắt đầu từ ngay bây giờ. Nhưng một vấn đề khác là văn bản bắt đầu hiển thị từ đầu trang. Nhưng tôi cần phải căn giữa theo chiều dọc của văn bản. Mặc dù tôi đã thử một số biến thể nhưng tôi không thể làm cho nó theo chiều dọc.cách căn chỉnh văn bản theo chiều dọc trung tâm trong android

Đây là mẫu tệp xml của tôi.

<LinearLayout 
      android:id="@+id/linearLayout5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:layout_marginBottom="23dp" 
       android:gravity="right" 
       android:padding="@dimen/padding_maintextview" 
       android:text="@string/text" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textSize="20sp" /> 
     </LinearLayout> 

Sự cố với chế độ xem văn bản ở trên.

Ở đây tôi đã đặt toàn bộ tệp xml.

<?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="wrap_content" 
    android:background="@drawable/page1background" 
    android:paddingRight="@dimen/padding_large" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="196dp" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:gravity="center_horizontal" 
     android:paddingTop="@dimen/padding_Title_Top" 
     android:text="@string/text" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textSize="20sp" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textView1" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" > 

     <View 
      android:id="@+id/view1" 
      android:layout_width="fill_parent" 
      android:layout_height="5dp" /> 
    </LinearLayout> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/linearLayout2" 
     android:layout_below="@id/linearLayout1" 
     android:layout_gravity="center" 
     android:padding="@dimen/padding_maintextview" > 

     <LinearLayout 
      android:id="@+id/linearLayout5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:layout_marginBottom="23dp" 
       android:gravity="right" 
       android:padding="@dimen/padding_maintextview" 
       android:text="@string/text" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textSize="20sp" /> 
     </LinearLayout> 
    </ScrollView> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" > 

     <View 
      android:id="@+id/view2" 
      android:layout_width="fill_parent" 
      android:layout_height="100dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" > 

     <ImageButton 
      android:id="@+id/back_arrow" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="30dp" 
      android:layout_marginRight="45dp" 
      android:layout_weight=".5" 
      android:background="@drawable/backbut" 
      android:contentDescription="@string/Description" 
      android:onClick="onClickBtn" 
      android:src="@drawable/backarrowpress" /> 

     <ImageButton 
      android:id="@+id/copyButton" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="45dp" 
      android:layout_weight=".5" 
      android:background="@drawable/copy" 
      android:contentDescription="@string/Description" 
      android:onClick="onClickBtn" /> 
    </LinearLayout> 

</RelativeLayout> 

Ai có thể chỉ cho tôi nơi tôi đã phạm sai lầm không? Tôi nghĩ vấn đề là rõ ràng. Nếu không cho tôi biết trong bình luận.

Theo Thông tư này, tôi có mã được cập nhật được thêm vào sau khi xem lại câu trả lời của bạn.

<?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="wrap_content" 
    android:background="@drawable/page1background" 
    android:paddingRight="@dimen/padding_large" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="196dp" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:gravity="center_horizontal" 
     android:paddingTop="@dimen/padding_Title_Top" 
     android:text="@string/text" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textSize="20sp" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textView1" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" > 

     <View 
      android:id="@+id/view1" 
      android:layout_width="fill_parent" 
      android:layout_height="5dp" /> 
    </LinearLayout> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@id/linearLayout2" 
     android:layout_below="@id/linearLayout1" 
     android:layout_gravity="center" 
     android:layout_centerInParent="true" 
     android:padding="@dimen/padding_maintextview" > 

     <LinearLayout 
      android:id="@+id/linearLayout5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center_vertical" 
       android:layout_marginBottom="23dp" 
       android:gravity="center_vertical|right" 
       android:padding="@dimen/padding_maintextview" 
       android:text="@string/text" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textSize="20sp" /> 
     </LinearLayout> 
    </ScrollView> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" > 

     <View 
      android:id="@+id/view2" 
      android:layout_width="fill_parent" 
      android:layout_height="100dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" > 

     <ImageButton 
      android:id="@+id/back_arrow" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="30dp" 
      android:layout_marginRight="45dp" 
      android:layout_weight=".5" 
      android:background="@drawable/backbut" 
      android:contentDescription="@string/Description" 
      android:onClick="onClickBtn" 
      android:src="@drawable/backarrowpress" /> 

     <ImageButton 
      android:id="@+id/copyButton" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="45dp" 
      android:layout_weight=".5" 
      android:background="@drawable/copy" 
      android:contentDescription="@string/Description" 
      android:onClick="onClickBtn" /> 
    </LinearLayout> 

</RelativeLayout> 

Nhưng tôi cũng ở trong tình huống tương tự. Không văn bản được theo chiều dọc làm trung tâm

Trả lời

92

Thuộc tính TextView của bạn cần phải được một cái gì đó như thế nào,

<TextView ... 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical|right" ../> 

Bây giờ, mô tả tại sao những cần phải được thực hiện,

android:layout_width="match_parent" 
android:layout_height="match_parent" 

Làm TextView của bạn để match_parent hoặc fill_parent nếu Bạn không muốn như vậy, match_parent bạn phải cung cấp một số giá trị được chỉ định cho layout_height để có được không gian cho trọng tâm dọc. android:layout_width="match_parent" cần thiết vì nó sắp xếp TextView của bạn ở bên phải để bạn có thể nhận ra sự tôn trọng bố cục bố cục của TextView.

Bây giờ, khoảng android:gravity làm cho nội dung của căn chỉnh TextView của bạn. android:layout_gravity làm cho căn chỉnh của TextView được tôn trọng với Bố cục cha mẹ của nó.

Cập nhật:

Như bên dưới nhận xét nói sử dụng fill_parent thay vì match_parent. (Sự cố trong một số thiết bị.)

+1

Vui lòng tránh sử dụng 'match_parent'.Sử dụng fill_parent hoặc wrap_content thay thế.Bởi vì khớp _parent không hỗ trợ trong một số thiết bị. –

+0

Tôi đã thử câu trả lời của bạn với match_parent và fill_parent vì ứng dụng của tôi hỗ trợ các phiên bản cũ hơn nhưng không có may mắn. Tôi vẫn phải đối mặt với cùng một vấn đề. Văn bản vẫn hiển thị ở trên cùng. – newday

+1

Chỉ dành cho mục đích gỡ lỗi và kiểm tra, bạn có thể viết các giá trị mã hoá cứng tại LinearLayout 'android: layout_height =" 200dp "' cha mẹ của TextView. – user370305

7

Cố gắng đặt android:gravity="center_vertical|right" bên trong parentLayout mẹ khác khi bạn ở trong RelativeLayout, bạn có thể đặt android:layout_centerInParent="true" bên trong scrollView.

+0

tôi đã làm nhưng vẫn còn vấn đề tương tự. – newday

+0

Đã sửa lỗi cho tôi, cảm ơn! – Rob

16

chỉ cần sử dụng như thế này để làm bất cứ điều gì để tập trung

android:layout_gravity="center" 
android:gravity="center" 

Cập nhật:

android:layout_gravity="center|right" 
android:gravity="center|right" 

Cập nhật: Chỉ cần loại bỏ MarginBottom từ TextView của bạn .. Làm như thế này ..cho TextView bạn

<LinearLayout 
     android:id="@+id/linearLayout5" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:gravity="center|right" 
      android:text="hello" 
      android:textSize="20dp" /> 
    </LinearLayout> 
+0

Cả Chế độ xem cha và con đều có cùng một 'android: layout_height =" wrap_content "'. Vì vậy, thay đổi không phản ánh. (Người dùng không thể nhận ra là trọng lực được đặt thành 'center_vertical'). – user370305

+0

bạn đã thử thực hiện các thay đổi đối với LinearLayout của mình chưa. Hãy thử rằng nó sẽ làm việc. cập nhật câu trả lời –

+0

thực hiện cả hai thay đổi đối với textView và LinearLayout –

0

Trong bố trí tương đối bạn cần xác định chiều cao TextView:

android:layout_height="100dp" 

Hoặc xác định dòng thuộc tính:

android:lines="3" 
+0

Thường tốt hơn 'android: lines =" 3 "' là 'android: maxLines =" 3 "', cung cấp bố cục linh hoạt hơn trong khi vẫn sửa chữa 'lỗi' được đề cập. – hardanger

5

Vấn đề là đệm của phông chữ trên TextView. Chỉ cần thêm vào chế độ xem văn bản của bạn:

android:includeFontPadding="false" 
+0

Có 'TextView' với' android: layout_height = "24dp" 'và' android: textSize = "20dp" 'và' android: gravity = "center_vertical" 'Tôi quan sát thấy văn bản đó vẫn được căn chỉnh dưới.' Android: includeFontPadding = "false" 'sửa lỗi của tôi! – foxanna

+0

Cảm ơn! Tôi không biết rằng phông chữ Roboto có đệm ... – Phil

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