android - layout_centerHorizo​​ntal 当另一个 View 位于其右侧时项目不居中

标签 android android-layout android-relativelayout

当我使用 layout_centerHorizo​​ntal 将 View 中的一个项目水平居中,而另一个项目位于它的右侧时,它没有居中。

enter image description here

但是当我删除第二个项目时,它会移动到中心。

enter image description here

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="4">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"
        android:gravity="center_horizontal">

    <TextView
        android:id="@+id/message"
        android:layout_width="240dp"
        android:layout_height="240dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#13D372"/>

    <TextView
        android:id="@+id/message2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="50dp"
        android:layout_toRightOf="@id/message"
        android:background="#125632"
        />

    </RelativeLayout>

</LinearLayout>

最佳答案

使用 android:gravity="center_horizontal"会将 child (您的两个 TextView)放置在其容器的水平中心。

从父布局中移除 android:gravity="center_horizo​​ntal" 并在子布局中保留 android:layout_centerHorizo​​ntal="true" (TextView)只居中这个。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="4">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6">

        <TextView
            android:id="@+id/message"
            android:layout_width="240dp"
            android:layout_height="240dp"
            android:layout_centerHorizontal="true"    <!--here-->
            android:layout_marginTop="50dp"
            android:background="#13D372"/>

        <TextView
            android:id="@+id/message2"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginTop="50dp"
            android:layout_toRightOf="@id/message"
            android:background="#125632"
            />

    </RelativeLayout>

</LinearLayout>

关于android - layout_centerHorizo​​ntal 当另一个 View 位于其右侧时项目不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33062260/

相关文章:

Android.Views.InflateException : Binary XML file line #17: Error inflating class android. 支持.design.internal.NavigationMenuItemView

Android:如何创建自定义通知并编辑其布局以编程方式添加 View ?

java - setMeasuredDimension() 中的尺寸不正确或尺寸计算不正确?

android - 在 Android 的图库 View 中添加 RelativeLayout 或 ListView?

Android:语音气球 - ImageView 重叠父级

android - 在android中重新启动 Activity 时取消初始化所有变量和函数值

android - 如何将 View 内部发生的事情记录为视频? (安卓)

android - 从警报对话框启动自定义对话框 - NullPointer 错误

Android - ListView 更改 View 属性

androidrelativelayout 两行,右边一列