एंड्रॉइड लेआउट में बाएं और दाएं किनारों पर पाठ्यपुस्तकों को संरेखित करना


158

मैं एंड्रॉइड के साथ शुरुआत कर रहा हूं। मुझे एक सरल लेआउट जाने में परेशानी हो रही है।

मैं एक पंक्ति में LinearLayoutदो की स्थिति का उपयोग करना चाहूंगा TextViews। एकTextView बाएं हाथ की तरफ, दूसरा दाएं हाथ की तरफ (फ्लोट करने के लिए समान: बाएं, फ्लोट: सीएसएस में दाएं)।

क्या यह संभव है, या क्या मुझे एक अलग उपयोग करने की आवश्यकता है ViewGroup इसे पूरा या आगे के लेआउट के घोंसले का है?

यहाँ मेरे पास अभी तक क्या है:

<?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:orientation="horizontal" android:padding="10sp">
<TextView android:id="@+id/mytextview1" android:layout_height="wrap_content" android:text="somestringontheleftSomestring" android:layout_width="wrap_content"/>
<TextView android:id="@+id/mytextview2" android:layout_height="wrap_content" android:ellipsize="end"
    android:text="somestringontheright" android:layout_width="wrap_content"/>
</LinearLayout>

जवाबों:


290

के RelativeLayoutसाथ प्रयोग करें layout_alignParentLeftऔर layout_alignParentRight:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="10dp">

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
        android:id="@+id/mytextview1"/>

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:id="@+id/mytextview2"/>

</RelativeLayout>

इसके अलावा, आपको अपने लेआउट के बजाय संभवतः dip(या dp) का उपयोग करना चाहिएspspपाठ सेटिंग्स के साथ-साथ स्क्रीन घनत्व को प्रतिबिंबित करें ताकि वे आमतौर पर केवल पाठ आइटम को आकार देने के लिए हों।


57
सामग्री को ओवरलैप होने से रोकने के लिए, पहले
टेक्स्टव्यू

डायनेमिक टेबल के लिए, आप कर सकते हैं: TableRow.LayoutParams col1Params = new TableRow.LayoutParams (); // लपेटें पंक्ति की सामग्री col1Params.height = LayoutParams.WRAP_CONTENT; col1Params.width = LayoutParams.WRAP_CONTENT; // स्तंभ col1Params.gravity = Gravity.LEFT के गुरुत्वाकर्षण को केंद्र में रखने के लिए गुरुत्वाकर्षण सेट करें;
सिद्धसिंह २४'१४

3
विभिन्न डिवाइस स्क्रीन के लिए बेहतर संगतता और समर्थन के लिए, "android: layout_alignParentEnd =" true "(जब लेआउट_alignParentRight का उपयोग कर रहे हैं) और" android: Layout_alignParentStart = "सही" का उपयोग करें (जब लेआउट_alignParentLeft का उपयोग करें)।
ivanleoncz

@ रोलिन_एस, आई लव यू।
वेगास

89

आप गुरुत्वाकर्षण संपत्ति का उपयोग "फ्लोट" विचारों के लिए कर सकते हैं।

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

    <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="horizontal">

        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:gravity="left"
            android:layout_weight="1"
            android:text="Left Aligned"
            />

        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_weight="1"
            android:text="Right Aligned"
            />
    </LinearLayout>

</LinearLayout>

1
दुर्भाग्य से ऐसा लगता है कि इससे दोनों आइटम एक ही पंक्ति में नहीं आते
Webnet

1
क्या गुरुत्वाकर्षण केवल "पाठ संरेखण" को प्रभावित नहीं करता है?
जोशुआ पिंटर

3
क्षमा करें, इसका कार्य। मैं एंड्रॉइड जोड़ना भूल गया था: लेआउट_वेट = "1"। जोड़ने के बाद उसका जुर्माना।
अब्दुल्ला उमेर

13

यह LinearLayout(कम ओवरहेड और रिलेटिव लेआउट विकल्प की तुलना में अधिक नियंत्रण) के साथ किया जा सकता है । शेष स्थान को दूसरा दृश्य दें ताकि gravityकाम हो सके। एपीआई 16 पर वापस परीक्षण किया गया।

सबूत

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aligned left" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="Aligned right" />
</LinearLayout> 

यदि आप पहले पाठ दृश्य के आकार को सीमित करना चाहते हैं, तो यह करें:

आवश्यकतानुसार वज़न समायोजित करें। सापेक्ष लेआउट आपको इस तरह प्रतिशत वजन सेट करने की अनुमति नहीं देगा, केवल एक विचार का एक निश्चित डीपी

प्रमाण २

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Aligned left but too long and would squash the other view" />

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="Aligned right" />
</LinearLayout>

8

रोलिन की टिप के साथ भी, डेव वेब के जवाब ने मेरे लिए काम नहीं किया। दाईं ओर TextViewका पाठ अभी भी बाईं ओर पाठ को ओवरलैप कर रहा थाTextView

मुझे अंततः वह व्यवहार मिला, जो मैं इस तरह से चाहता था:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:padding="10dp">

<TextView 
    android:id="@+id/mytextview1"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" />

<TextView 
    android:id="@+id/mytextview2"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/mytextview1"
    android:gravity="right"/>

</RelativeLayout>

ध्यान दें कि mytextview2 के "android:layout_width"रूप में सेट किया गया है "match_parent"

आशा है कि यह किसी की मदद करता है!


4

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Hello world" />

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Gud bye" />


1

मामले में आप सामग्री को लपेटने के लिए बाएँ और दाएँ तत्व चाहते हैं, लेकिन मध्य स्थान है

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</LinearLayout>

0

इसे पूरा करने के कई अन्य तरीके हैं, मैं कुछ ऐसा करूंगा।

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

    <TextView
        android:id="@+id/textRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="30dp"
        android:text="YOUR TEXT ON THE RIGHT"
        android:textSize="16sp"
        android:textStyle="italic" />


    <TextView
        android:id="@+id/textLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="30dp"
        android:text="YOUR TEXT ON THE LEFT"
        android:textSize="16sp" />
</RelativeLayout>

0

डेव वेब के जवाब ने मेरे लिए काम किया। धन्यवाद! यहाँ मेरा कोड, आशा है कि यह किसी की मदद करता है!

<RelativeLayout
    android:background="#FFFFFF"
    android:layout_width="match_parent"
    android:minHeight="30dp"
    android:layout_height="wrap_content">
    <TextView
        android:height="25dp"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="ABA Type"
        android:padding="3dip"
        android:layout_gravity="center_vertical"
        android:gravity="left|center_vertical"
        android:layout_height="match_parent" />
    <TextView
        android:background="@color/blue"
        android:minWidth="30px"
        android:minHeight="30px"
        android:layout_column="1"
        android:id="@+id/txtABAType"
        android:singleLine="false"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content" />
</RelativeLayout>

चित्र: छवि


0

यहां छवि विवरण दर्ज करें

यह कोड नियंत्रण को दो समान पक्षों में विभाजित करेगा।

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout">
    <TextView
        android:text = "Left Side"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight = "1"
        android:id = "@+id/txtLeft"/>

    <TextView android:text = "Right Side"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight = "1"
        android:id = "@+id/txtRight"/>
    </LinearLayout>
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.