क्षैतिज LinearLayout में राइट संरेखित करें बटन


130

यदि आप संलग्न छवि को देखते हैं। मुझे अपने बटन को सही संरेखित करने की आवश्यकता है लेकिन किसी कारण से यह 'गुरुत्वाकर्षण: सही' के साथ काम नहीं कर रहा है ...

Add रद्द करें

यहाँ उस लेआउट के लिए मेरा कोड है:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

कोई काम क्यों नहीं कर रहा है ?!


6
इस मामले में सापेक्ष लेआउट आज़माएं। एलएल अभ्यस्त काम,
एअनकिट

4
यदि आप LinearLayout का उपयोग करना चाहते हैं, तो दो इनर लाइनरआउट्स का उपयोग करें और लेआउट_वेट का उपयोग लेआउट_ग्रेविटी के साथ करें। यह ठीक काम करेगा।
वामसी चल्ला

@VamsiChalla एक के साथ संभव है, नीचे मेरा नया उत्तर जांचें।
ट्वीस्टेरोब

जवाबों:


137

उसके लिए नीचे दिए गए कोड का उपयोग करें

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="9dp"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/stitch_button"
        android:text="@string/add" />

</RelativeLayout>

3
ऐसा लगता है कि इस मामले में बटन के लिए विशेषता 'layout_alignParentRight' उपलब्ध नहीं है? पदावनत हो सकती है?

20
linearlayout के बजाय संबंध का उपयोग करते हुए आरयू?
दीपक केशरिया

1
आह बकवास .. इस बारे में क्षमा करें, मैंने पूरी तरह से अतीत को देखा। धन्यवाद!

4
यह अभी भी मुझे आश्चर्यचकित करता है कि कैसे कुछ ऐसा लगता है जो इतना आसान हो जाता है कि एक रैखिक रेखा का उपयोग करने की कोशिश करना मुश्किल हो जाता है।
अल्वारोसिस्टिस्बेन

2
सही "LinearLayout | शीर्ष: @DipakKeshariya भविष्य दर्शकों के लिए, आप इस्तेमाल कर सकते हैं" layout_gravity = एंड्रॉयड "
SupimpaAllTheWay

145

एकल LinearLayoutसमाधान। केवल एक साधारण रिक्ति दृश्य जोड़ रहा है:
(कोई नहीं लगता है कि इस एक का उल्लेख किया गया है, केवल अधिक जटिल मल्टी-लेआउट समाधान।)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <!-------------------------  ADDED SPACER VIEW -------------------->
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <!------------------------- /ADDED SPACER VIEW -------------------->

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

"हाइलाइटेड" दृश्य पर ध्यान दें, मैंने कुछ और संशोधित नहीं किया। ऊँचाई = 0 सुनिश्चित करता है कि यह दिखाई नहीं दे रहा है। चौड़ाई = 0 है क्योंकि चौड़ाई LinearLayoutवजन = 1 के आधार पर निर्धारित होती है । इसका मतलब यह है कि स्पेसर दृश्य जितना संभव हो उतना दिशा में (अभिविन्यास) में खिंचाव करेगा LinearLayout

ध्यान दें कि आप का उपयोग करना चाहिए android.widget.Spaceया android.support.v4.widget.Spaceइसके बजायView अगर आपके एपीआई स्तर और / या निर्भरता इसे अनुमति देते हैं। Spaceसस्ते तरीके से नौकरी हासिल करता है, क्योंकि यह केवल उपाय करता है और कुछ भी करने की कोशिश नहीं Viewकरता है; यह इरादा व्यक्त करने के लिए अधिक स्पष्ट है।


2
बस Android जोड़ें: लेआउट_वेट = "1" से R.id.lblExpenseCancel और वह सब।
hrules6872

1
@h_rules जो यदि आप हैक करना चाहते हैं तो काम करते हैं, लेकिन कल्पना करें कि जब आप TextView में पृष्ठभूमि जोड़ते हैं तो क्या होता है।
ट्वीस्टरेब

2
मुझे आश्चर्य है कि यह उत्तर नीचे क्यों मिला, यह काम करता है और यह अच्छा है, मैं इसे निश्चित रूप से कोशिश करने वाला हूं।
एल्वेडिन हैम्ज़ैजिक

यह एक मोबाइल डेवलपर के रूप में एकदम सही है
शिवानंद दरूर

यदि आप एक स्पेसर जोड़ते हैं, तो विभिन्न स्क्रीन आकारों में आपके पास अलग परिणाम है। तो इसका आदर्श समाधान नहीं है।
जियोमिंट


28

मुझे पता है कि यह सवाल कुछ समय पहले पूछा गया था, लेकिन मैंने पहले भी ऐसा किया है और यह आइटम संरेखित करते समय अधिक नियंत्रण की अनुमति देता है। यदि आप इसे वेब प्रोग्रामिंग की तरह देखते हैं, तो यह मदद करता है। आप बस एक और घोंसला बनाते हैं LinearLayoutजिसमें इसके अंदर आपका बटन होगा। फिर आप बटन के लेआउट के गुरुत्वाकर्षण को बदल सकते हैं और इसे दाईं ओर जा सकते हैं जबकि TextViewअभी भी बाईं ओर है।

इस कोड को आज़माएं:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"             
android:orientation="horizontal" 
android:layout_marginTop="35dp">

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cancel" 
    android:textColor="#404040"         
    android:layout_marginLeft="10dp" 
    android:textSize="20sp" 
    android:layout_marginTop="9dp"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"                    
        android:text="@string/add" 
        android:layout_gravity="right" 
        android:layout_marginRight="15dp"/>

</LinearLayout>

आपको नेस्टेड लेआउट पर पैडिंग के साथ खेलना पड़ सकता है ताकि यह काम करे कि आप इसे कैसे चाहते हैं।


2
आपको अपने नेस्टेड लेआउट के लिए एक फ्रेम-आउट का उपयोग करना चाहिए। एक एकल तत्व की स्थिति के लिए यह क्या है। और यह भी, यह एक वजन दे ताकि यह माता-पिता के दाहिने हिस्से को भर दे। वैसे भी सापेक्ष लेआउट का उपयोग नहीं करने के लिए +1।
हजलमर

10

इसको आजमाओ

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_gravity="right" 
    android:layout_height="wrap_content"             
    android:orientation="horizontal"  >

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"   
   android:orientation="horizontal" >

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="9dp"
    android:text="cancel"
    android:textColor="#ffff0000"
    android:textSize="20sp" />

<Button
    android:id="@+id/btnAddExpense"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="15dp"
     android:textColor="#ff0000ff"
    android:text="add" />

 </RelativeLayout>
  </LinearLayout>

9

जैसा कि पहले एक दो बार उल्लेख किया गया है: रैखिकलेयूट से रिलेटिवलैटआउट काम करने के लिए स्विच करने के लिए, लेकिन आप इसे टालने के बजाय समस्या को हल कर सकते हैं। LinearLayout प्रदान करता है उपकरण का उपयोग करें: TextView को एक भार = 1 दें (नीचे कोड देखें), आपके बटन का वजन 0 या कोई भी नहीं रहना चाहिए। इस मामले में TextView शेष सभी जगह ले जाएगा, जिसका उपयोग आपके TextView या ButtonView की सामग्री को प्रदर्शित करने के लिए नहीं किया जाता है और आपके बटन को दाईं ओर धकेलता है।

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="35dp">

        <TextView
            android:id="@+id/lblExpenseCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:textColor="#404040"
            android:layout_marginLeft="10dp"
            android:textSize="20sp"
            android:layout_marginTop="9dp"

            **android:layout_weight="1"**

            />

        <Button
            android:id="@+id/btnAddExpense"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:background="@drawable/stitch_button"
            android:layout_marginLeft="10dp"
            android:text="@string/add"
            android:layout_gravity="right"
            android:layout_marginRight="15dp" />

    </LinearLayout>

2
यह सबसे अच्छा जवाब है। मैं तब तक कुछ ऐसा ही लिखना चाहता था जब तक मुझे आपकी पोस्ट नहीं मिली।
कोडिंगडेव

यह RelativeLayoutमेमोरी मुद्दों के कारण स्विच करने से बेहतर है , क्योंकि RelativeLayoutखपत की तुलना में अधिक मेमोरी होती हैLinearLayout
थंडरवाइरिंग

7

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

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_gravity="right" 
android:layout_height="wrap_content"             
android:orientation="horizontal" 
android:layout_marginTop="35dp">

2
क्या तब TextView सही के साथ संरेखित नहीं होगा? मुझे बस बटन को संरेखित करने की आवश्यकता है ...

1
यदि आपको केवल बटन को संरेखित करने की आवश्यकता है, तो RelativeLayout का उपयोग करें।
शुभम

1
यदि आप सभी बटन संरेखित करना चाहते हैं, तब भी यह काम नहीं करता है (एपीआई 16)। बच्चों के विचार अभी भी बाईं ओर संरेखित हैं।
m0skit0

5
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"             
    android:orientation="horizontal" 
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:text="@string/cancel" 
        android:textColor="#404040"         
        android:layout_marginLeft="10dp" 
        android:textSize="20sp" 
        android:layout_marginTop="9dp"/>              

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="wrap_content"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"                    
        android:text="@string/add" 
        android:layout_gravity="right" 
        android:layout_marginRight="15dp"/>

</LinearLayout>

इससे आपकी समस्या दूर हो जाएगी


3

यदि आप नहीं चाहते हैं, या नहीं कर सकते हैं, RelativeLayout का उपयोग करते हैं, तो आप बटन को रेखीयआउटआउट में अभिविन्यास "ऊर्ध्वाधर" और चौड़ाई "fill_parent" के साथ लपेट सकते हैं।

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_marginTop="35dp">

  <TextView
      android:id="@+id/lblExpenseCancel"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/cancel"
      android:textColor="#404040"
      android:layout_marginLeft="10dp"
      android:textSize="20sp"
      android:layout_marginTop="9dp" />

  <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">
     <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />
  </LinearLayout>
</LinearLayout> 

ऐसा इसलिए है क्योंकि यदि रैखिकअनुक्रम क्षैतिज है, तो गुरुत्वाकर्षण केवल विचारों को लंबवत रूप से प्रभावित करेगा। और यदि अभिविन्यास 'ऊर्ध्वाधर' है, तो गुरुत्वाकर्षण केवल विचारों को क्षैतिज रूप से प्रभावित करेगा। LinearLayout अभिविन्यास / गुरुत्वाकर्षण स्पष्टीकरण पर अधिक जानकारी के लिए यहां देखें ।


2

बस एंड्रॉइड जोड़ें : गुरुत्वाकर्षण = "सही" यह लाइन पैरेंट लेआउट यह काम करेगा।

<LinearLayout
        android:id="@+id/withdrawbuttonContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        **android:gravity="right"**
        android:weightSum="5"
        android:visibility="visible">

        <Button
            android:id="@+id/bt_withDraw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/BTN_ADD"
            app:delay="1500" />

        <Button
            android:id="@+id/btn_orderdetail_amend"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/BTN_CANCEL"
            app:delay="1500" />
</LinearLayout>

0

मैंने 4 TextViewएस के साथ एक समान लेआउट का उपयोग किया है । दो TextViewएस को बाईं ओर और दो TextViewएस को दाएं से जोड़ा जाना चाहिए। तो, यहाँ मेरा समाधान है, यदि आप LinearLayoutsअकेले उपयोग करना चाहते हैं।

<?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="wrap_content"
    android:orientation="horizontal"
    android:baselineAligned="false"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="0dip"
        android:layout_weight="0.50"
        android:layout_height="match_parent"
        android:gravity="left"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_level"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/level"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_level_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_weight="0.50"
        android:layout_height="match_parent"
        android:gravity="right"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/time"
            android:textAppearance="?android:attr/textAppearanceMedium"
             />

        <TextView
            android:id="@+id/textview_fragment_mtfstatus_time_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium" 
            />
    </LinearLayout>

</LinearLayout>

0

आप RelativeLayout का उपयोग कर सकते हैं या अपने बटन के मूल लेआउट पर गुरुत्वाकर्षण = "सही" सेट कर सकते हैं।


0

मुझे पता है कि यह पुराना है, लेकिन यहाँ एक और एक रैखिक लेआउट में एक होगा:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="35dp">

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cancel"
    android:textColor="#404040"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:layout_marginTop="9dp" />

<Button
    android:id="@+id/btnAddExpense"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:background="@drawable/stitch_button"
    android:layout_marginLeft="10dp"
    android:text="@string/add"
    android:layout_gravity="center_vertical|bottom|right|top"
    android:layout_marginRight="15dp" />

कृपया ध्यान दें कि सिर्फ गुरुत्वाकर्षण के विपरीत लेआउट_गैविटी।


क्या हो रहा है center_vertical|bottom|top? वे मेरे लिए अनन्य संरेखण गुरुत्वाकर्षण की तरह दिखते हैं।
TWiStErRob

0

आपको मुख्य लेआउट के रूप में लिनियरलेआउट के बजाय रिलेटिवेलआउट का उपयोग करना चाहिए। यदि आप Relativelayout को मुख्य के रूप में उपयोग करते हैं तो आसानी से दाईं ओर बटन को संभालें क्योंकि रिश्तेदार लेआउट हमें संरेखित करें दाएं, बाएं, ऊपर और नीचे।


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