Android লেআউটে বাম এবং ডান প্রান্তগুলিতে পাঠ্যদর্শনগুলি সারিবদ্ধ করা


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
ওভারল্যাপিং থেকে সামগ্রীগুলি রোধ করতে, প্রথম পাঠ্যদর্শনটিতে 'অ্যান্ড্রয়েড: বিন্যাস_toLeftOf = "@ + আইডি / মাইটেক্সটভিউ 2"' যুক্ত করতে চাইবে
রোলিন_

গতিশীল টেবিলগুলির জন্য, আপনি এটি করতে পারেন: টেবিলরো.লয়আউটপ্যারামস কোল 1 পারামস = নতুন টেবিলরো.লয়আউটপ্যারাম (); // সারিটির সামগ্রীটি গুছিয়ে ফেলুন কল 1Params.height = লেআউটপ্যারামস W Wrap_CONTENT; col1Params.width = লেআউটপ্যারামস W Wrap_CONTENT; // কলাম 1 প্যারামস.গ্রাভিটি = গ্র্যাভিটি.এলএফটি এর মাধ্যাকর্ষণ কেন্দ্রকে কেন্দ্র করে মাধ্যাকর্ষণ সেট করুন;
সিদ্ধসিংহে

3
বিভিন্ন ডিভাইসের স্ক্রিনের জন্য আরও ভাল সামঞ্জস্যতা এবং সহায়তার জন্য, "অ্যান্ড্রয়েড: লেআউট_লাইন্পেন্টারেন্ট রাইট ব্যবহার করার সময়" এবং "অ্যান্ড্রয়েড: লেআউট_্যালাইন্প প্যারেন্টস্টার্ট =" সত্য "" (লেআউট_আলাইনপ্যারেন্টলিফ্ট ব্যবহার করার সময়) ব্যবহার করুন।
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
দুর্ভাগ্যক্রমে মনে হচ্ছে এটি উভয় আইটেমকে একই লাইনে রাখার কারণ নয়
ওয়েবনেট

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> 

আপনি যদি প্রথম পাঠ্য দর্শনটির আকার সীমাবদ্ধ করতে চান তবে এটি করুন:

প্রয়োজনীয় ওজন সামঞ্জস্য করুন। আপেক্ষিক বিন্যাস আপনাকে এ জাতীয় শতাংশের ওজন নির্ধারণ করতে দেয় না, কেবলমাত্র দেখার মধ্যে একটির স্থির ডিপি

প্রমাণ 2

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

নোট করুন যে মাইটেক্সটভিউ 2 "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>
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.