【问题标题】:android:layout_alignParentRight="true" - editText still appears on the left of the layout - Android / XMLandroid:layout_alignParentRight="true" - editText 仍然出现在布局的左侧 - Android / XML
【发布时间】:2013-12-25 01:01:25
【问题描述】:

我有一个带有两个editText 字段的RelativeLayout,我试图与屏幕右侧对齐。我所看到的和我记得的 android:layout_alignParentRight="true" 应该可以解决问题,但由于某种原因,它们仍然出现在屏幕的左侧。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/firstName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="First Name"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/lastName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Last Name"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Email Address"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:hint="Phone Number"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/userName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@null"
        android:ems="10"
        android:hint="USERNAME"
        android:inputType="textPersonName"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:ems="10"
        android:hint="PASSWORD"
        android:inputType="textPassword"
        android:textColor="#000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:background="@null"
            android:text="" />
    </LinearLayout>

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Submit" />

</RelativeLayout>

编辑:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"

    android:scaleType="fitXY" 
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/firstName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="First Name"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/lastName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Last Name"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Email Address"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:hint="Phone Number"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/userName"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@null"
        android:ems="10"
        android:textColor="@color/black"
        android:hint="USERNAME"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/password"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:background="@null"
        android:ems="10"
        android:textColor="@color/black"
        android:hint="PASSWORD"
        android:inputType="textPassword" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:background="@null"
            android:text="" />
    </LinearLayout>

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Submit" />

</RelativeLayout>

编辑后的外观:

【问题讨论】:

    标签: android xml android-relativelayout android-ui android-xml


    【解决方案1】:

    editTexts 的宽度为 match_parent,它们的文本左对齐。因此,即使编辑文本在右侧,它也从左侧开始(因为它填充了其父级的宽度)。把宽度改小一点,你会发现你的代码可以工作了。

    【讨论】:

    • 它们仍然没有显示在屏幕右侧(我发布了更新的屏幕截图 w 代码)
    • 呃,用户名在右边..?你只是把它弄得太小了。以 250dp 为例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多