【问题标题】:TextView's and EdidText's not align as expectedTextView 和 EdidText 未按预期对齐
【发布时间】:2015-08-14 14:33:57
【问题描述】:

所以我试图做一个简单的注册活动,但我的观点与预期不一致。

XML:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mylifeinformationsharedsocial.SignUpActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/username_id"
    android:text="@string/username_text"
    android:textSize="20sp"
    />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/username_edit_text_id"
    android:hint="@string/username_hint_texts"
    android:layout_toRightOf="@id/username_id"
    android:layout_toEndOf="@id/username_id"
    />

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/password_id"
    android:text="@string/password_text"
    android:textSize="20sp"
    android:layout_below="@id/username_edit_text_id"
    />

<EditText
    android:id="@+id/password_edit_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:hint="@string/passworde_hint_texts"
    android:layout_toRightOf="@id/password_id"
    android:layout_toEndOf="@id/password_id"
    android:layout_below="@id/username_edit_text_id"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/email_id"
    android:text="@string/email_text"
    android:textSize="20sp"
    android:layout_below="@id/password_id"
    />

<EditText
    android:id="@+id/email_edit_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:hint="@string/email_hint_text"
    android:layout_toRightOf="@id/email_id"
    android:layout_toEndOf="@id/email_id"
    android:layout_below="@id/password_edit_text_id"
    />

<Button
    android:id="@+id/sign_up_button_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sign_up_button_text"
    android:layout_below="@id/email_id"
    />

图形布局:

我似乎找不到它有什么问题。任何帮助将不胜感激。 如图所示,问题在于电子邮件向下。 它非常困难,但显然不是。

【问题讨论】:

  • 使用 LinearLayout 更好地对齐。
  • @Rubanraj 我知道这个答案会来哈哈。我想用相对布局来做。
  • 谢谢你,伙计.. :-) 你找到了我.. @God

标签: java android android-relativelayout graphical-layout-editor


【解决方案1】:

您的password_text 被声明为低于username_edit_text_id,而它希望低于username_id

Android 会接受您提供的所有规格,并尽最大努力按照您的要求去做,但如果规格不一致或无法满足,那么它就会出错。

【讨论】:

  • 它没有帮助,但你是对的。我想我不能用相对布局来做到这一点。我将切换到 LineaLayout。谢谢。
【解决方案2】:

也许你可以这样尝试:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/username_id"
    android:text="@string/username_text"
    android:textSize="20sp"
    />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/username_edit_text_id"
    android:hint="@string/username_hint_texts"
    android:layout_toRightOf="@id/username_id"
    android:layout_toEndOf="@id/username_id"
    />

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/password_id"
    android:text="@string/password_text"
    android:textSize="20sp"
    android:layout_below="@id/username_edit_text_id"
    />

<EditText
    android:id="@+id/password_edit_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:hint="@string/passworde_hint_texts"
    android:alignRight = "@id/username_edit_text_id"
    android:layout_below="@id/username_edit_text_id"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/email_id"
    android:text="@string/email_text"
    android:textSize="20sp"
    android:layout_below="@id/password_id"
    />

<EditText
    android:id="@+id/email_edit_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:hint="@string/email_hint_text"
    android:alignRight = "@id/password_edit_text_id"
    android:layout_below="@id/password_edit_text_id"
    />

<Button
    android:id="@+id/sign_up_button_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sign_up_button_text"
    android:layout_below="@id/email_id"
    />

我没有使用toRightOf,而是使用alignRight,所以所有EditText都会右对齐。

这很适合您的情况(如果您只有这些行),否则您应该将所有 EditText 对齐到最右边的那一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2022-01-21
    • 2019-12-25
    • 2012-07-15
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多