2014-03-25 33 views
1

的TextView的一行文字垂直居中我有一個100dp lineSpacingExtra一個多行編輯文本。如何讓Android中

現在文本顯示在行的頂部。而且光標的高度與線條高度相同,它比文字的高度高得多。

我該如何讓文本顯示在行的中間? 或者如果我可以調整一行文本的填充頂部或填充底部。

注意: 我的意思是不使用android:gravity =「center_vertical」在TextView中使文本中心垂直。 我想在文本行中垂直顯示文本中心。

XML是如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:background="@android:color/transparent" 
     android:fadingEdge="vertical" 
     android:gravity="top" 
     android:inputType="textCapSentences|textMultiLine|textShortMessage" 
     android:lineSpacingExtra="100dp" 
     android:longClickable="false" 
     android:minLines="10" 
     android:paddingBottom="5dp" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:paddingTop="5dp" 
     android:scrollbars="vertical" 
     android:text="Text ABCDE\n Line2" 
     android:textAlignment="center" 
     android:textSize="16sp" /> 
</LinearLayout> 

任何幫助理解。 THX!

+1

張貼您的xml代碼 – Lokesh

+0

在這裏顯示您的xml和快照。 – Piyush

+0

只有EditText沒有辦法,你應該創建一個LinearLayout或RelativeLayout來環繞EditText,將LinearLayout或RelativeLayout的重力設置爲Gravity.Center,將EditText的高度設置爲Wrap_Content,它應該可以工作 –

回答

0

在此附上我的結果:
在應用程序中沒有辦法做到這一點。
由於TextView在框架中的一行頂部繪製文本。
如果你想要顯示的文字在一行中垂直居中顯示的視覺效果,必須修改框架代碼。

文件位置是:
框架/基/核心/ JAVA /安卓/文本/ StaticLayout.java

的變化:

-  lines[off + DESCENT] = below + extra; 
+  lines[off + DESCENT] = below + extra/2;//Show the text center vertically in the line. 

請糾正我,如果出現問題或者有一個更好的這樣做的方法。

0

編輯答案:

輸出是:

enter image description here

代碼(從我自己的XML標籤,所以改變你的ID):

 <LinearLayout 
       android:id="@+id/layout_feedback" 
       android:layout_width="401dp" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tv_six" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="27dp" 
       android:background="@drawable/textfield_activated_holo_dark" 
       android:gravity="center_horizontal" 
       android:orientation="horizontal" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0" 
        android:gravity="center_horizontal" 
        android:hint="Em " 
        android:visibility="invisible" 
        android:textColor="@color/white" 
        android:textSize="24sp" /> 

       <EditText 
        android:id="@+id/ed_feedback_know" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/textfield_empty_holo_dark" 
        android:focusable="true" 
        android:text="Text ABCDE\n Line2" 
        android:focusableInTouchMode="true" 
        android:inputType="textEmailAddress" 
        android:textColor="@android:color/black" 
        android:textSize="24sp" /> 
      </LinearLayout> 
+0

感謝您的回覆。但是我需要的是在其行的中心顯示文本,而不是在TextView的中心顯示文本。 –

+0

嗨WJ S,感謝您指出確切的問題,我編輯了我的答案。希望它能幫助你 –

+0

對不起,我錯過了強調編輯文本是多行的,lineSpacingExtra是100dp。如果你這樣做,很容易發現文本顯示在行的頂部。我想用大lineSpacingExtras將文本放在該行的中心。無論如何,感謝您的熱情幫助。 :) –