简体   繁体   English

如何集中所有设备的线性布局

[英]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, 并使LinearLayout居中,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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