রৈখিক বিন্যাসের ভিতরে কীভাবে বিষয়বস্তুটি কেন্দ্র করবেন?


155

আমি ImageViewএকটি LinearLayoutঅনুভূমিক এবং উল্লম্বভাবে একটি অভ্যন্তরকে কেন্দ্র করার চেষ্টা করছি , তবে আমি এটি করতে পারি না। আমি কেন এটির RelativeLayoutজন্য ব্যবহার করছি না তার মূল কারণ হ'ল আমার দরকার layout_weight(আমার Activityচারটি কলাম রয়েছে যা সমানভাবে বিভক্ত হওয়া উচিত, এবং বিভিন্ন স্ক্রিন প্রস্থের প্রতিও প্রতিক্রিয়াশীল, প্রতিটি কলাম একটি ImageViewকেন্দ্রিক এবং আনসার্টেড রয়েছে) need

এখানে এখন পর্যন্ত আমার এক্সএমএল:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:baselineAligned="false"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_speak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_speak" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_readtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_readtext" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_edit" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_config"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_config" />
    </LinearLayout>
</LinearLayout>

আপনার
— ইচ্ছামতো

উত্তর:


381

android:gravity তার বাচ্চাদের সারিবদ্ধতা পরিচালনা করে,

android:layout_gravity নিজেই প্রান্তিককরণ পরিচালনা করে।

সুতরাং এর মধ্যে একটি ব্যবহার করুন।

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:baselineAligned="false"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imageButton_speak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_speak" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imageButton_readtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_readtext" />
    </LinearLayout>

    ...
</LinearLayout>

অথবা

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:baselineAligned="false"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_speak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_speak" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageButton_readtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/image_bg"
            android:src="@drawable/ic_readtext" />
    </LinearLayout>

    ...
</LinearLayout>

"অ্যান্ড্রয়েড: লেআউট_গ্রাভিটি নিজের যত্ন নেয়" " অভিভাবকরা লেআউট মাধ্যাকর্ষণ ব্যবহার করে বলে সংশোধন করবে।
— আতাউলম

36

android:layout_gravity লেআউট নিজেই ব্যবহার করা হয়

আপনার android:gravity="center"বাচ্চাদের জন্য ব্যবহার করুনLinearLayout

সুতরাং আপনার কোডটি হওয়া উচিত:

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1" >

7

আমি এখানে উল্লিখিত সমাধানগুলি চেষ্টা করেছিলাম কিন্তু এটি আমার সাহায্য করে না। আমি মনে করি সমাধানটি মান হিসাবে layout_widthব্যবহার করতে হবে wrap_content।

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1" >

5

এখানে কিছু নমুনা কোড। এটি আমার পক্ষে কাজ করেছে।

<LinearLayout
    android:gravity="center"
    >
    <TextView
        android:layout_gravity="center"
        />
    <Button
        android:layout_gravity="center"
        />
</LinearLayout>

সুতরাং আপনি এর সমস্ত বিষয়বস্তু (টেক্সটভিউ এবং বোতাম) এর কেন্দ্রে রাখার জন্য লিনিয়ার লেআউটটি ডিজাইন করছেন এবং তারপরে পাঠ্যদর্শন এবং বোতামটি লিনিয়ার লেআউটটির কেন্দ্রের তুলনায় স্থাপন করা হয়েছে।


1
সহজ। 'মাধ্যাকর্ষণ' বৈশিষ্ট্যটি একটি ভিউয়ের সামগ্রীকে প্রভাবিত করে। সুতরাং এক্ষেত্রে লিনিয়ারলআউট এর সামগ্রী কী? একটি পাঠ্যদর্শন এবং একটি বোতাম। কার্যত আপনি লেআউট ম্যানেজারকে লিনিয়ার লেআউটের যে কোনও বিষয়বস্তুকে কেন্দ্রিয় করতে নির্দেশ দিচ্ছেন।
— পিসোডেক্স

2

android:gravity একটি লেআউট এর শিশুদের সারিবদ্ধ করার জন্য ব্যবহার করা যেতে পারে।

android:layout_gravity এর পিতামাতার মধ্যে নিজেকে সারিবদ্ধ করতে যে কোনও দৃশ্যে ব্যবহার করা যেতে পারে।

দ্রষ্টব্য: যদি স্ব বা শিশুরা প্রত্যাশা অনুযায়ী কেন্দ্র না করে থাকে তবে প্রস্থ / উচ্চতা কিনা তা পরীক্ষা করে match_parentঅন্য কোনও কিছুর পরিবর্তিত হয়

আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.