2011-03-29 16 views
22

मेरे पास एक क्षैतिज स्क्रॉलव्यू है जो टेक्स्टव्यू के साथ गतिशील रूप से भरा हुआ है। कभी-कभी एक, दो, तीन या अधिक तत्व होते हैं। जो मैं खोज रहा हूं वह तत्वों की संख्या के बावजूद आइटम केंद्रित करने का एक तरीका है।केंद्रित तत्वों के साथ क्षैतिज स्क्रॉलव्यू

उदाहरण के लिए, एक तत्व के साथ: तीन तत्वों के साथ

| ---- MenuA --- MenuB ---- | 

:

| --------- MenuA --------- | 

दो तत्वों के साथ

| - MenuA - MenuB - MenuC - | 

पांच तत्वों के साथ (MenuD और menue छिपे हुए हैं) :

| MenuA - MenuB - MenuC - Me| 

अद्यतन: यहां दो मेनू के साथ एक साधारण लेआउट है। मैं मेनू ए और मेनूबी कैसे केंद्रित करूं?

एंड्रॉयड: layout_gravity = "center_horizontal" संपादित करें: ०३.३०:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <HorizontalScrollView android:id="@+id/horizontalScrollView1" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     <LinearLayout android:id="@+id/linearLayout1" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" 
      android:orientation="horizontal"> 
      <TextView android:text="MenuA" android:id="@+id/textView1" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
      <TextView android:text="MenuB" android:id="@+id/textView2" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 
+0

तो आपका 'स्कॉल व्यू' क्षैतिज (स्क्रीन से बड़ा) है, आइटम स्क्रीन केंद्रित हैं? – Phonon

+0

हां, मेरा क्षैतिज स्क्रॉलव्यू क्षैतिज है, और आइटम क्षैतिज रूप से केंद्रित होना चाहिए –

उत्तर

29

गुरुत्वाकर्षण विशेषता का उपयोग

मुझे मिल गया! केवल गुरुत्वाकर्षण को ऊपरी रैखिक स्तर पर सेट करना पड़ा: पाठकों को पढ़ने के लिए और अधिक आरामदायक बनाने के लिए आपको बस इतना करना है कि थोड़ा पैडिंग/मार्जिन जोड़ें!

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" > 

    <HorizontalScrollView 
     android:id="@+id/horizontalScrollView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" > 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/TextView04" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MenuB" > 
      </TextView> 

      <TextView 
       android:id="@+id/TextView03" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MenuB" > 
      </TextView> 

      <TextView 
       android:id="@+id/TextView02" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MenuB" > 
      </TextView> 

      <TextView 
       android:id="@+id/TextView01" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MenuB" > 
      </TextView> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MenuA" > 
      </TextView> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MenuB" > 
      </TextView> 
     </LinearLayout> 
    </HorizontalScrollView> 

</LinearLayout> 

मुझे पोस्ट रखें!

+2

मैंने सेटिंग्स एंड्रॉइड की कोशिश की: layout_gravity = "center_horizontal" और टेक्स्ट व्यू अभी भी –

+1

पर स्थित हैं, क्या आप अपना पूरा लेआउट भेज सकते हैं? – olamotte

+0

यह वास्तव में केवल 'एंड्रॉइड होना चाहिए: गुरुत्वाकर्षण = "center_horizontal" ', चूंकि' लेआउट_ग्रैविटी 'केवल क्षैतिज स्क्रॉल दृश्य को माता-पिता के क्षैतिज केंद्र पर रखेगा, लेकिन ऑफहैंड, मुझे पूरा यकीन नहीं है कि आप काम करेंगे जैसा कि आप उम्मीद कर रहे हैं .. – kcoppock

संबंधित मुद्दे