【问题标题】:Centering 2 different layouts in a xml file在 xml 文件中居中 2 个不同的布局
【发布时间】:2018-05-06 15:37:56
【问题描述】:

我很难将我的 2 个线性布局居中。第一个线性布局使国家代码选择器和编辑文本在同一行。第二个线性布局假设将按钮放在第一个线性布局之下。但如果我将按钮放在第一个线性布局下,它就不会再居中了。

<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/ghostWhiteColor"
    android:orientation="vertical"

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="10dp"
        android:gravity="center"
        android:id="@+id/linearLayout1">


        <com.hbb20.CountryCodePicker
            android:id="@+id/ccp"
            android:layout_width="86dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            app:ccp_showFullName="false"
            app:ccp_showNameCode="false"
            app:ccp_showPhoneCode="true" />

        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/register_password"
            android:layout_width="260dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:layout_marginTop="-2dp"
            android:hint="@string/hint_phone"
            android:inputType="phone"
            android:padding="10dp"
            android:singleLine="true"
            app:met_floatingLabel="normal"
            app:met_helperText="Enter your phone number."
            app:met_helperTextAlwaysShown="true" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="10dp"
        android:gravity="center">

        <com.rey.material.widget.Button
            android:id="@+id/button_register_accept"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="@color/holo_blue_light"
            android:padding="10dp"
            android:paddingEnd="50dp"
            android:text="@string/btn_accept"
            android:textColor="@color/ghostWhiteColor" />

    </LinearLayout>

</RelativeLayout>

不居中的示例。 https://puu.sh/yruSe/0721745d0b.png

【问题讨论】:

  • 你能把你想要的截图放上来吗?
  • @diegoveloper 更新了当前线程的未居中示例。
  • 好的,在下面查看我的解决方案

标签: android xml android-layout layout


【解决方案1】:

从此替换您的父布局:

<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/ghostWhiteColor"
    android:orientation="vertical">

到这里:

<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:gravity="center"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

【讨论】:

    【解决方案2】:
    Try this :)
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/ghostWhiteColor"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="10dp">
    
    
                <com.hbb20.CountryCodePicker
                    android:id="@+id/ccp"
                    android:layout_width="86dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    app:ccp_showFullName="false"
                    app:ccp_showNameCode="false"
                    app:ccp_showPhoneCode="true" />
    
                <com.rengwuxian.materialedittext.MaterialEditText
                    android:id="@+id/register_password"
                    android:layout_width="260dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="-2dp"
                    android:hint="@string/hint_phone"
                    android:inputType="phone"
                    android:padding="10dp"
                    android:singleLine="true"
                    app:met_floatingLabel="normal"
                    app:met_helperText="Enter your phone number."
                    app:met_helperTextAlwaysShown="true" />
    
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="10dp">
    
                <com.rey.material.widget.Button
                    android:id="@+id/button_register_accept"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="4dp"
                    android:background="@color/holo_blue_light"
                    android:padding="10dp"
                    android:text="@string/btn_accept"
                    android:textColor="@color/ghostWhiteColor" />
    
            </LinearLayout>
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 2016-12-20
      相关资源
      最近更新 更多