android - 使用 relativelayout 对齐在中心对齐的 2 行控件

标签 android android-relativelayout

Android Studio 0.5.4

你好,

我有一个包含 2 列 EditText 的 relativeLayout。

我希望第二行从屏幕中心开始,第一列中的 EditText 从左侧开始并停在中心。

我一直在摆弄 centerInParent 和 centerHorizo​​natal toLeftOf,但看不出它是正确的。我不想使用 LinearLayout 并希望在可能的情况下仅使用 RelativeLayout 来做到这一点。所以 linearLayout 不是我的选择。

非常感谢您的任何建议。 截屏: enter image description here

我使用的样式:

<resources>
    <style name="addressBookLand">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textSize">22sp</item>
        <item name="android:layout_marginBottom">4dp</item>
    </style>
</resources>

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fffc">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:gravity="center"
        android:inputType="text"
        android:text="Address Book App"
        android:textStyle="bold|italic" />

    <EditText
        android:id="@+id/etFirstName"
        style="@style/addressBookLand"
        android:hint="Enter your first name"
        android:inputType="text"
        android:layout_below="@id/tvTitle"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/etLastName"/>

    <EditText
        android:id="@+id/etLastName"
        style="@style/addressBookLand"
        android:hint="Enter your last name"
        android:inputType="text"
        android:layout_below="@id/tvTitle"
        android:layout_centerHorizontal="true"/>

    <EditText
        android:id="@+id/etAddressLine"
        style="@style/addressBookLand"
        android:hint="Enter your address"
        android:inputType="text"
        android:layout_below="@id/etFirstName"/>

    <EditText
        android:id="@+id/etDistrict"
        style="@style/addressBookLand"
        android:hint="Enter your district"
        android:inputType="text"
        android:layout_below="@id/etLastName"
        android:layout_toRightOf="@id/etAddressLine"/>

    <EditText
        android:id="@+id/etCity"
        style="@style/addressBookLand"
        android:hint="Enter your city"
        android:inputType="text"
        android:layout_below="@id/etAddressLine"/>

    <EditText
        android:id="@+id/etPostCode"
        style="@style/addressBookLand"
        android:hint="Enter your postcode"
        android:inputType="number"
        android:layout_below="@id/etDistrict"
        android:layout_toRightOf="@id/etCity"/>

    <EditText
        android:id="@+id/etPhoneNumber"
        style="@style/addressBookLand"
        android:hint="Enter your phone number"
        android:inputType="phone"
        android:layout_below="@id/etCity"/>

    <EditText
        android:id="@+id/etEmailAddress"
        style="@style/addressBookLand"
        android:hint="Enter your email Address"
        android:inputType="textEmailAddress"
        android:layout_below="@id/etPostCode"
        android:layout_toRightOf="@id/etPhoneNumber"/>

    <Button
        android:id="@+id/btnSubmit"
        style="@style/addressBookLand"
        android:text="Submit"
        android:layout_below="@id/etPhoneNumber"
        android:layout_toLeftOf="@id/btnCancel"
        android:layout_alignParentLeft="true"/>

    <Button
        android:id="@+id/btnCancel"
        style="@style/addressBookLand"
        android:onClick="onClickCancel"
        android:text="Cancel"
        android:layout_below="@id/etEmailAddress"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

最佳答案

如果您绝对不想使用 LinearLayout那么一个优雅的解决方案可能是创建一个 TextView用 0 宽度和高度作为中心点的标记,并用它来定位你的另一个 View在它周围。例如;

 <TextView
    android:id="@+id/centre_marker"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_centerHorizontal="true"/>

因为它的宽度为 0(如果你想在你的 EditText 之间留一个空格,你可以给这个标记一个宽度,或者在你的其他 View 上使用边距),它将或多或少正好在中心。因此,您可以将其左侧的所有组件与右侧对齐,而右侧的所有组件都与左侧对齐。例如;

 <EditText
    android:id="@+id/etFirstName"
    style="@style/addressBookLand"
    android:hint="Enter your first name"
    android:inputType="text"
    android:layout_below="@id/tvTitle"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@id/centre_marker"/>
<EditText
    android:id="@+id/etLastName"
    style="@style/addressBookLand"
    android:hint="Enter your last name"
    android:inputType="text"
    android:layout_below="@id/tvTitle"
    android:layout_toRightOf="@id/centre_marker"/>

或者,正如其他人提到的,两个大小相等的垂直 LinearLayouts在水平 LinearLayout 内只要您不打算使布局变得更复杂或不那么严格,就会得到您需要的结果。

希望对您有所帮助。如果您需要澄清,可以给我留言,我会尽我所能解释。

关于android - 使用 relativelayout 对齐在中心对齐的 2 行控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22957212/

相关文章:

java - 为 ListView 设置适配器时出现 NullPointerException

java - Android webview录音不起作用

android - 绑定(bind)一个 View 以在 RelativeLayout 中拖动

android - 找不到符号变量 FLAG_ACTIVITY_CLEAR_TASK

java - 当我收到通知时如何制作弹出 Activity ?

android - 设置一个 TextView 完全是照片的特定部分?

java - NullPointerException - android.view.Window.findViewById(int) - 为什么?

android - 当布局宽度为 720 时,RelativeLayout.getWidth 返回 0

android - RecyclerView java.lang.IndexOutOfBoundsException : Inconsistency detected. 无效的 View 持有者适配器位置。由于更改 getItemCount()

android - 如何在具有多个 TextView 的相对布局中保持 TextView 居中对齐