2011-04-11 10 views
13

मैं एंड्रॉइड के लिए काफी नया हूं और नमूना ऐप करने की कोशिश कर रहा हूं जहां 3 तत्व (2 बटन और 1 टेक्स्ट व्यू) क्षैतिज रूप से रखा गया है। मुझे पाठ में बाईं ओर और दाईं ओर दिए गए दो बटनों के साथ केंद्र में रहने की आवश्यकता है।एंड्रॉइड बाएं, केंद्र और दायां संरेखण

उदा।

|<Button> <Text>  <Button>| 

यदि आप इससे मेरी सहायता कर सकते हैं तो सराहना करें।

अग्रिम धन्यवाद

+0

यदि आप हमें अब तक कोड दिखाते हैं, तो आपकी मदद करना आसान होगा। – Phonon

उत्तर

20

संपादित करें: मुझे विश्वास है कि यह आपकी समस्या का समाधान करेगा। नीचे कोड देखें। मुझे बताएं क्या इससे मदद मिलती है! मुझे पता है कि इससे मेरी मदद मिली है और मैं इसका इस्तेमाल करूँगा। अगर यह काम करता है तो इसे उत्तर के रूप में स्वीकार करें! =)

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="1"> 
    <TableRow> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button1" 
      android:id="@+id/button1button"></Button> 
     <EditText 
      android:layout_height="wrap_content" 
      android:id="@+id/firstedittext" 
      android:layout_width="wrap_content"></EditText> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button2" 
      android:id="@+id/button2button"></Button> 
    </TableRow> 
</TableLayout> 

संपादित करें: जोड़ा रिश्तेदार लेआउट देखने के लिए कि यह आप के लिए काम करता है, मुझे पता है अगर यह काम करता है। वांछित दूरी प्राप्त करने के लिए आपको "10px" समायोजित करने की आवश्यकता होगी। मैं देख रहा हूं कि ऐसा करने का एक बेहतर तरीका है, जहां दूरी अधिक गतिशील है।

स्क्रीन के दाएं और बाएं किनारे के साथ संरेखित करने के लिए आप बटन "alignParentLeft" और "alignParentRight" बना सकते हैं। हालांकि, मुझे अभी भी दो विचारों के बीच पाठ प्राप्त करने में परेशानी हो रही है।

आप इसे लेआउट के लिए अपनी एक्सएमएल फ़ाइल में कर सकते हैं। इसे एक क्षैतिज लेआउट बनाएं और पहले बटन को पहले जोड़ें, फिर टेक्स्ट व्यू, फिर दूसरा बटन। उदाहरण के लिए जल्द ही एक्सएमएल पोस्ट करेगा।

<RelativeLayout 
     android:id="@+id/LinearLayout01" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button1" 
      android:layout_alignParentLeft="true" 
      android:layout_marginRight="10px" 
      android:id="@+id/button1button"></Button> 
     <EditText 
      android:layout_height="wrap_content" 
      android:id="@+id/firstedittext" 
      android:layout_toRightOf="@id/button1button" 
      android:layout_marginRight="10px" 
      android:layout_width="wrap_content"></EditText> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/firstedittext" 
      android:text="button2" 
      android:id="@+id/button2button"></Button> 
    </RelativeLayout> 
+0

उत्तर के लिए धन्यवाद। मैंने इस तरह से कोशिश की। लेकिन वे ठीक से गठबंधन नहीं करते हैं। (दो बटनों को स्क्रीन के बाएं और दाएं किनारे पर गठबंधन करने की आवश्यकता है।) और मैंने चौड़ाई को पैरेंट करने के लिए सेट किया है। – budsiya

+0

अब मैं देखता हूं, मैंने आपकी पोस्ट संशोधित की है ताकि तस्वीर बहुत बेहतर दिखाई दे। यह समीक्षा की प्रतीक्षा कर रहा है। जब इसकी समीक्षा हो जाती है तो आपको इसे अपनी पोस्ट में दिखाना चाहिए। जैसा कि आप कह रहे हैं, वास्तव में दृश्य के तत्वों को संरेखित करने के साथ मैंने कभी भाग्य नहीं लिया है। हालांकि, जब मुझे मौका मिलता है तो मैं इसमें देखता हूं और देखता हूं कि मैं इसे काम कर सकता हूं, क्योंकि मैं उस प्रश्न का एक सरल जवाब भी ढूंढ रहा हूं। – prolink007

+0

आपके समर्थन के लिए बहुत बहुत धन्यवाद। वास्तव में इसकी सराहना करते हैं। एक बार जब मैं घर आज रात जाऊं तो मैं दोनों तरीकों का प्रयास करूंगा। आपको परिणाम बताएगा। टीएक्स! – budsiya

19

यह चाल करेगा। आपको जो चाहिए वह पूर्ण गुरुत्वाकर्षण है। यह सभी एंड्रॉइड स्क्रीन आकारों के साथ भी काम करना चाहिए। मुझे नहीं लगता कि आपको किसी मार्जिन या पैडिंग का उपयोग करने की आवश्यकता होगी, आपके बच्चों में सामग्री होने के बाद वे आपको गड़बड़ कर देंगे (मार्जिन उन्हें चुनने के मामले में रिश्तेदार हैं, उर्फ: जब आपके पास कम टेक्स्ट होता है तो आप एक छोटा मार्जिन चाहते हैं। .. जो अक्सर अनुवाद, नए कार्यों, प्रतिस्थापन, आदि के दौरान होता है ..)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="button1" 
     android:layout_alignParentLeft="true"/> 
    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/firstedittext" 
     android:layout_centerInParent="true"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="button2" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 
+0

धन्यवाद एडिसन ... मैं आज रात इसे आजमाउंगा और आपको परिणाम बता दूंगा। आपकी सहायता की सराहना। :) – budsiya

+0

मैं उस "layout_centerInParent" के लिए हर जगह देख रहा था और इसे खोजने में असमर्थ था। मैं मान रहा था कि यह अन्य नामों के समान नामकरण का पालन करेगा "layout_alignParentLeft" और "layout_alignParentRight"।तो, मैं "layout_alignParentCenter", lol की तलाश में था। – prolink007

+0

@ prolink007 क्या आप नवीनतम एसडीके का उपयोग कर रहे हैं? मुझे यकीन नहीं है कि एसडीके ने लेआउट_एक्स क्या दिया है, लेकिन यदि आप केवल एंड्रॉइड टाइप करते हैं: और प्रतीक्षा करें और फिर "एल" टाइप करें, ग्रहण को आपके लिए फ़िल्टर करना चाहिए ... मैं आमतौर पर बस याद रखने की कोशिश करने के बजाय "l" टाइप करता हूं पूरी बात क्योंकि ... मेरी याददाश्त T_T बेकार है। – Edison

0

यह लाइनरलायआउट पर मेरा कोड है। उम्मीद है कि यह आपकी मदद करेगा!

<?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="horizontal" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:text="Button1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:ems="10" 
      android:text="Chao cac ban" > 

      <requestFocus /> 
     </EditText> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="end" 
      android:text="Button2" /> 
    </LinearLayout> 

</LinearLayout> 
संबंधित मुद्दे