在android线性布局中居中内容的最佳方式

时间:2017-09-17 11:07:13

标签: android android-layout layout android-linearlayout

我使用android developer studio创建了以下启动画面布局。我想将包含两个文本视图和图像视图的内容居中。以下是XML。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_gradient">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView id="@+id/splashscreen"
        android:layout_width="100dp"
        android:layout_height="250dp"
        android:paddingBottom="100dp"
        android:layout_centerInParent="true"
        android:gravity="center_vertical|center_horizontal"
        android:src="@drawable/splash_logo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample"
        android:layout_centerInParent="true"
        android:fontFamily="@string/font_family_thin"
        android:paddingTop="90dp"
        android:paddingRight="90dp"
        android:textSize="35dp"
        android:textColor="#fff"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="App"
        android:layout_centerInParent="true"
        android:fontFamily="@string/font_family_regular"
        android:textSize="35dp"
        android:paddingTop="90dp"
        android:paddingLeft="100dp"
        android:textColor="#fff"
        />

</RelativeLayout>

这将如下图所示。

enter image description here

这是将我的内容集中在一起的正确方法,还是有更好的方式来实现我想要的东西?请指导我。提前谢谢。

2 个答案:

答案 0 :(得分:1)

如果您使用填充来使元素居中,那么这是错误的方式,因为您将在不同的屏幕上获得不同的结果。

看到这个替代方案,它不是唯一的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash_gradient"
    android:gravity="center">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageView 
            android:id="@+id/splashscreen"
            android:layout_width="100dp"
            android:layout_height="250dp"
            android:paddingBottom="100dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/splash_logo" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/splashscreen"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal"
            android:layout_marginTop="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sample"
                android:textSize="35dp"
                android:textColor="#fff"
                android:layout_marginEnd="15dp"
                android:layout_marginRight="10dp"
                android:fontFamily="@string/font_family_thin"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="App"
                android:layout_centerInParent="true"
                android:textSize="35dp"
                android:textColor="#fff"
                android:fontFamily="@string/font_family_regular"
                />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

答案 1 :(得分:0)

首先不要使用填充或边距来对齐兄弟视图或布局中的视图,它在1个设备中看起来很棒,但在具有不同屏幕尺寸的其他设备中不会。使用layout_align属性。学习线性布局属性here。然后你就能发现自己的最佳方式。

我的解决方案

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

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="250dp" 
            android:layout_centerInParent="true"
            />
        <LinearLayout
            android:layout_centerInParent="true"
            android:layout_below="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/sample"
                android:layout_alignBottom="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sample"
                android:layout_centerInParent="true"
                android:textSize="35dp"
                android:textColor="#000"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" App"
                android:textSize="35dp"
                android:textColor="#000"
                />

        </LinearLayout>




</RelativeLayout>