首页 > 解决方案 > 如何在 RealativeLayout 中将 LinearLayout 居中

问题描述

我正在尝试将 LinearLayout (在 RelativeLayout 内)居中,无论出于何种原因 - 它只是不会居中。它一直粘在左边。非常感谢帮助!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/main_background"
   tools:context=".SlideActivity"
   android:layout_centerInParent="true">

   <android.support.v4.view.ViewPager
       android:id="@+id/slideViewPager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_centerInParent="true"></android.support.v4.view.ViewPager>

   <LinearLayout
       android:id="@+id/dotsLayout"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/slideViewPager"
       android:layout_alignParentStart="true"
       android:layout_centerInParent="true"
       android:orientation="horizontal"
       android:padding="20dp"
       android:layout_gravity="center" />
</RelativeLayout>

https://drive.google.com/file/d/1MjhGpGhqBM_SJa1j3soEL31ZzoAkf6pg/view?usp=sharing 底部的 3 个点应该居中。但是他们坚持向左...

标签: javaandroidxml

解决方案


试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_background"
    tools:context=".SlideActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/slideViewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        />

    <LinearLayout
        android:layout_below="@id/slideViewPager"
        android:id="@+id/dotsLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:orientation="horizontal"
        android:padding="20dp"/>
</RelativeLayout>


推荐阅读