2013-01-01 10 views
38

में पाठ लंबवत केंद्र को संरेखित करने के लिए कैसे करें मेरे पास अरबी टेक्स्ट है, इसलिए मैंने सही तरफ से टेक्स्ट शुरू करने के लिए सही गुरुत्वाकर्षण सेट किया है। टेक्स्ट अभी से शुरू होता है। लेकिन एक और मुद्दा पृष्ठ के शीर्ष से पाठ प्रस्तुत करना शुरू होता है। लेकिन मुझे पाठ को लंबवत रूप से केंद्रित करने की आवश्यकता है। हालांकि मैंने कई भिन्नताओं की कोशिश की, लेकिन मैं इसे लंबवत केंद्र नहीं बना सका।एंड्रॉइड

यहां मेरी एक्सएमएल फ़ाइल का नमूना है।

<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> 

समस्या उपरोक्त टेक्स्टव्यू के साथ है।

यहां मैंने पूरी एक्सएमएल फ़ाइल डाली है।

<?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> 

क्या कोई मुझे दिखा सकता है कि मैंने गलती कहाँ की है? मुझे लगता है कि समस्या स्पष्ट है। अगर टिप्पणियों में मुझे नहीं बताओ।

हेरिथ मैंने आपके उत्तरों की समीक्षा के बाद अद्यतन कोड जोड़ा है।

<?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> 

लेकिन मैं एक ही स्थिति में हूं। कोई पाठ लंबवत रूप से केंद्रित है

उत्तर

92

आपका TextView गुण कुछ की तरह होने की जरूरत है,

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

अब, विवरण क्यों इन किया जा करने की जरूरत है,

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

match_parent या fill_parent यदि करने के लिए अपने TextView बनाता है आप इसे match_parent की तरह नहीं बनना चाहते हैं, आपको layout_height पर कुछ निर्दिष्ट मान देना होगा ताकि यह ऊर्ध्वाधर केंद्र गुरुत्वाकर्षण के लिए स्थान प्राप्त कर सके। android:layout_width="match_parent" आवश्यक है क्योंकि यह आपके टेक्स्ट व्यू को दाएं तरफ संरेखित करता है ताकि आप टेक्स्ट व्यू के अभिभावक लेआउट के संबंध में पहचान सकें।

अब, इसके बारे में android:gravity जो आपके टेक्स्ट व्यू संरेखण की सामग्री बनाता है। android:layout_gravity अपने माता-पिता लेआउट के सम्मान में TextView के संरेखण को बनाता है।

अद्यतन: टिप्पणी कहते हैं

नीचे के रूप में fill_parent बजाय match_parent का उपयोग करें। (कुछ डिवाइस में समस्या।)

+1

कृपया 'match_parent' के उपयोग से बचें। इसके बजाय fill_parent या wrap_content का उपयोग करें। क्योंकि किसी डिवाइस में _parent का समर्थन नहीं हुआ है। –

+0

मैंने अपने उत्तर को match_parent और fill_parent के साथ भी करने का प्रयास किया क्योंकि मेरा ऐप पुराने संस्करणों का समर्थन करता है लेकिन कोई भाग्य नहीं है। मुझे अभी भी एक ही समस्या का सामना करना पड़ रहा है। पाठ अभी भी शीर्ष पर दिखाया गया है। – newday

+1

बस डीबग और परीक्षण उद्देश्य के लिए आप अपने LinearLayout 'एंड्रॉइड पर हार्ड कोड किए गए मान लिख सकते हैं: layout_height =" 200dp "' TextView के पैरेंट। – user370305

7

प्रयास करें और कुछ माता पिता LinearLayout अंदर android:gravity="center_vertical|right" डाल करने के लिए के रूप में आप RelativeLayout अंदर हैं आप अपने scrollView अंदर android:layout_centerInParent="true" डाल सकते हैं।

+0

मैंने किया लेकिन अभी भी एक ही मुद्दा है। – newday

+0

मेरे लिए इसे फिक्स किया, धन्यवाद! – Rob

16

सिर्फ केंद्र के लिए कुछ भी

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

अद्यतन बनाने के लिए इस तरह का उपयोग करें:

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

अपडेट किया गया: बस अपने TextView से MarginBottom को दूर .. इस तरह करते हैं ..अपने TextView

<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

दोनों अभिभावक और बाल दृश्य में एक ही 'एंड्रॉइड है: layout_height =" wrap_content "'। तो परिवर्तन परिलक्षित नहीं होता है। (उपयोगकर्ता को गुरुत्वाकर्षण के रूप में पहचाना नहीं जा सकता है 'center_vertical' पर सेट किया गया है)। – user370305

+0

क्या आपने अपने LinearLayout में परिवर्तन करने का प्रयास किया है। कोशिश करो कि यह काम करेगा। उत्तर –

+0

को अपडेट करने से टेक्स्टव्यू और लीनियरलाइट –

0

रिश्तेदार लेआउट में के लिए आप TextView ऊँचाई निर्दिष्ट की जरूरत है:

android:layout_height="100dp" 

या निर्दिष्ट लाइनों विशेषता:

android:lines="3" 
+0

अक्सर एंड्रॉइड से बेहतर: लाइन = " 3 "' है 'एंड्रॉइड: maxLines = "3" ', प्रश्न में' बग 'को ठीक करने के दौरान एक और अधिक लचीला लेआउट प्रदान करता है। – hardanger

5

समस्या TextView पर फ़ॉन्ट का गद्दी है। बस अपने टेक्स्टव्यू में जोड़ें:

android:includeFontPadding="false" 
+0

' एंड्रॉइड के साथ' टेक्स्ट व्यू 'रखने के बाद: layout_height = "24dp" 'और' android: textSize = "20 डीपी" 'और' एंड्रॉइड: गुरुत्वाकर्षण = "center_vertical" 'मैंने देखा कि पाठ अभी भी नीचे गठबंधन किया गया था। 'एंड्रॉइड: शामिलफॉन्टपैडिंग =" झूठी "' मेरी समस्या तय! – foxanna

+0

धन्यवाद! मुझे नहीं पता था कि रोबोटो फ़ॉन्ट में पैडिंग थी ... – Phil