एंड्रॉइड - सेंटर टेक्स्ट व्यू क्षैतिज रूप से रैखिकलेआउट में


111

मेरे पास निम्नलिखित मूल लेआउट हैं

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_bar_background">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:padding="10dp"
        android:text="HELLO WORLD" />

    </LinearLayout>
<LinearLayout>

ऐसा लगता है कि xml सही है लेकिन पाठ बाईं ओर संरेखित है। टेक्स्टव्यू माता-पिता की संपूर्ण चौड़ाई को लेता है और टेक्स्टव्यू को केंद्रित करने के लिए सेट किया गया है। पता नहीं है कि समस्या क्या है...


2
एंड्रॉइड आज़माएं: आपके
टेक्स्टव्यू के

जवाबों:


227

क्या हो रहा है कि चूंकि TextView इनर लीनियर लयआउट की पूरी चौड़ाई को भर रहा है, यह पहले से ही लेआउट के क्षैतिज केंद्र में है । जब आप इसका उपयोग android:layout_gravityकरते हैं तो विजेट को एक पूरे के रूप में, गुरुत्वाकर्षण के स्थान पर रखा जाता है। पूरे विजेट केंद्र को रखने के बजाय जो आप वास्तव में करने की कोशिश कर रहे हैं वह उस सामग्री को केंद्र में रखना है जिसे पूरा किया जा सकता है android:gravity="center_horizontal"और android:layout_gravityविशेषता को हटाया जा सकता है।


3
अगर मैं सही ढंग से समझता हूं कि वह "एंड्रॉइड: लेआउट_प्रवेशन =" फिल_परेंट "को" रैप_ कॉन्टेंट "में बदल सकता है और फिर एंड्रॉइड: लेआउट_ग्रेविटी =" सेंटर_हेरॉर्गेनल "का उपयोग कर सकता है? क्या मैं सही हूं?
पॉल ब्रूसकिनस्की 20

1
@bluesm नहीं, आंतरिक लिनेरलआउट अपने आप में वह स्थान रखने की अनुमति नहीं देता है जो दृश्य से भरा नहीं है (रिक्त लियर लयआउट के मामले पर विचार नहीं कर रहा है)। इस प्रकार android:layout_widthवसीयत का मान (लेआउट के बाद) समान होगा। चूंकि TextView की चौड़ाई भीतरी LinearLayout की साथ के बराबर है TextView प्रभावी रूप से है android:layout_gravityके मूल्यों left, rightऔर centerएक ही समय में।
दान एस

1
के लिए ImageViewअपने layout_gravityऔर के लिए TextViewअपनी gravityक्या है केवल काम करता है। Android महान है! सभी सुंदर एसओ पदों के लिए धन्यवाद जिसके बिना विकास असंभव था।
अतुल

1
के बारे में मत भूलो android:layout_width="match_parent"
Choletski 15

24
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bar_background">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:padding="10dp"
    android:text="HELLO WORLD" />

</LinearLayout>


24

यदि आप <TextView>केंद्र में सेट करते हैं <Linearlayout>तो पहले android:layout_width="fill_parent"अनिवार्य रूप से
किसी अन्य गुरुत्वाकर्षण का उपयोग करने की कोई आवश्यकता नहीं है

    <LinearLayout
            android:layout_toRightOf="@+id/linear_profile" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:orientation="vertical"
            android:gravity="center_horizontal">
            <TextView 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="It's.hhhhhhhh...."
                android:textColor="@color/Black"

                />
    </LinearLayout>


4

बस उपयोग करें: Android: layout_centerHor क्षैतिज = "सत्य"

यह पूरे टेक्स्टव्यू को केंद्र में रखेगा

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.