父级布局的水平和垂直中心

时间:2017-01-12 10:01:43

标签: android android-layout

我正在Android中创建用户个人资料屏幕。在此屏幕中有用户配置文件壁纸和用户配置文件图像。这里的灰色背景是配置文件壁纸,红色背景将配置文件图像。我需要将红色bg布局设置为灰色bg布局的水平和垂直中心..

enter image description here

--------------------------------------------------------
| USER_ID     | CAR_ID     | MODEL_ID    | VARIANT_ID  |
--------------------------------------------------------
| 1           | 1          | 2           | 3           |
--------------------------------------------------------
| 1           | 1          | 2           | 2           |
--------------------------------------------------------
| 3           | 2          | 1           | 1           |
--------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

您可以将父线性LinearLayout的重力设置为居中(只需添加1行):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlActivityNotificationsAndProfile">
<include layout="@layout/layout_tool_bar"/>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/profile_wallpaper_container_height"
    android:id="@+id/rlProfileWallpaper"
    android:layout_below="@+id/toolbar">
    <LinearLayout
        android:layout_width="match_parent"
        android:background="#646464"
        android:orientation="vertical"
        android:layout_height="@dimen/profile_wallpaper_height"
        android:gravity="center">
        <LinearLayout
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:background="#A52525">
        </LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/tools"
        android:id="@+id/fabEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/base_ten_dp"
        android:layout_marginEnd="@dimen/base_ten_dp"
        android:layout_marginStart="@dimen/base_ten_dp"
        android:scaleType="center"
        android:src="@drawable/add"
        app:elevation="@dimen/priority_1_elevation"
        app:fabSize="1"/>
</RelativeLayout>
</LinearLayout>