简体   繁体   中英

how to centralize linearlayout with items for all devices

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:layout_marginLeft="70dp"
    android:layout_marginRight="70dp"

    tools:context="com.example.sepia2.myapplication.Main3Activity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/link0"
            android:id="@+id/link0"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:textColor="#e67d0a"

            android:layout_marginLeft="10dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
           />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/link1"
                android:id="@+id/link1"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="10dp"
                android:textAppearance="?android:attr/textAppearanceLarge"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/link2"
                    android:id="@+id/link2"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="10dp"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                   />
</linearlayout>

Basically my point is that how can i centralize this layout? what is the accepted approach.

basically what i have done is this (can this approach break down in other screens?)

 android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"

but i have to adjust it. how can it automatically get in the center of the screen across all devices big or small?

You need to set gravity to linear layout

android:gravity="Center" //  this will make all child into center of screen

and to make LinearLayout to center,

android:layout_gravity="Center"

Reference

只需将其设置为layout_width="wrap_content"layout_gravity="center" ..希望它可以工作。

查看此博文,以阐明gravity / layout_gravity工作原理http://sandipchitale.blogspot.ru/2010/05/linearlayout-gravity-and-layoutgravity.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM