2012-11-20 6 views
21

के अंदर एक टेक्स्टव्यू को कैसे केंद्रित करें मेरे पास निम्न लेआउट है जो मैं टेक्स्टव्यू केंद्र और दृश्य के मध्य में दिखाना चाहता हूं। मैं इसे कैसे प्राप्त कर सकता हूं?एक लाइनरलेआउट

मैंने ग्राफिकल दृश्य में लेआउट को देखते समय विभिन्न गुरुत्वाकर्षण विशेषताओं को समायोजित करने का प्रयास किया है, लेकिन कुछ भी इसे बदलने के लिए प्रतीत नहीं होता है। मैं केंद्र में टेक्स्टव्यू चाहता हूं जो मैंने किया है लेकिन दृश्य के आधा रास्ते नीचे है।

धन्यवाद मैट।

<?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" 
    android:background="@drawable/carefreebgscaledalphajpg" > 



    <TextView 
     android:id="@+id/textviewdatefornocallspage" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="You have no calls for " 
     android:textSize="25sp" /> 

</LinearLayout> 

उत्तर

38

गुरुत्वाकर्षण को केंद्र में सेट करें अपने LinearLayout टैग में r:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:gravity="center" 
    android:background="@drawable/carefreebgscaledalphajpg" > 

या एक RelativeLayout इस तरह का उपयोग करें:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/carefreebgscaledalphajpg" > 



    <TextView 
     android:id="@+id/textviewdatefornocallspage" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="You have no calls for " 
     android:textSize="25sp" /> 

</RelativeLayout> 
+0

धन्यवाद काम करता है ठीक काम करता है अब – turtleboy

+1

RelativeLayout समाधान काम किया मेरे लिए, लेकिन टेक्स्ट व्यू में मुझे चौड़ाई और ऊंचाई दोनों के लिए 'wrap_content' का उपयोग करना पड़ा –

+0

@Jose_GD: दोनों मामलों में यह आपके लिए काम करेगा :)। चौड़ाई के लिए विशेषता 'match_parent' का उपयोग करने के मामले में, बस' layout_gravity = "center" 'जोड़ें। बस इतना ही ;) – Houcine

0

android:gravity="center_horizontal" अपने LinearLayout में जोड़े और अपने TextView की wrap_content करने के लिए अपने TextView के अपने TextView के layout_width रूप android:layout_width="wrap_content"

<?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" 
    android:gravity="center_horizontal" 
    android:background="@drawable/carefreebgscaledalphajpg" > 



    <TextView 
     android:id="@+id/textviewdatefornocallspage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="You have no calls for " 
     android:textSize="25sp" /> 

</LinearLayout> 
0

सेट चौड़ाई में परिवर्तन और android:gravity=center_horizontal करने के लिए अपने LinearLayout सेट

0

ऐसा लगता है कि आप के लिए TextView

दस्तावेज़ देख सकेंगे android:layout_gravity हैं: developer.android.com

0

यदि आपके अभिविन्यास लंबवत लाइनरलेआउट में, आप android:layout_gravity="center" द्वारा "क्षैतिज केंद्र" में टेक्स्टव्यू डाल सकते हैं। टेक्स्टव्यू को लंबवत रूप से केंद्रित करने के लिए आपको match_parent पर textview के layout_height को सेट करने और एंड्रॉइड सेट करने की आवश्यकता है: गुरुत्वाकर्षण "केंद्र" पर। यदि आप इस लेआउट में एक से अधिक दृश्यों का उपयोग करना चाहते हैं, तो आपको RelativeLayout का उपयोग करना चाहिए और android:layout_centerInParent="true" सेट करना चाहिए।

0

उपयोग सापेक्ष लेआउट, यह हमेशा और अधिक सटीक और लचीला लेआउट

<?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:orientation="vertical" > 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:src="@drawable/ic_launcher" /> 
</RelativeLayout> 
5

सेट android:gravity="center" रेखीय लेआउट में देता है और wrap_content के रूप में पाठ दृश्य की ऊंचाई और चौड़ाई रखने के लिए।

यह समाधान मेरे लिए काम किया।

2

प्रयास करें इस

सरल मैं कई जवाब की कोशिश की है, लेकिन सभी उत्तर काम नहीं कर रहे ...... अंत में इस विधि के लिए मुझे

     <LinearLayout 
         android:layout_below="@+id/iv_divider" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical" 
         android:layout_marginTop="@dimen/_10dp"> 
         <TextView 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:text="@string/mycredits" 
          android:textColor="@color/colorGray" 
          android:gravity="center" 
          android:textSize="@dimen/_25dp" /> 
        </LinearLayout> 
संबंधित मुद्दे