【问题标题】:Android RelativeLayout View OrderAndroid RelativeLayout 视图顺序
【发布时间】:2014-12-07 18:41:49
【问题描述】:

关于从 xml 文件中绘制 android 视图的顺序已经有几个问题得到了解答,我已经阅读了它们并尝试了他们的解决方案,但没有成功。我有一个带三个孩子的 RelativeLayout,一个 ImageView、一个按钮和一个 LinearLayout。我希望 LinearLayout 位于顶部,另外两个位于背景中。根据Defining Z order of views of RelativeLayout in Android,兄弟姐妹是按照从xml文件中读取的顺序绘制的,所以我将LinearLayout放在文件的最后,但它仍然在后面绘制。这是我的文件:

<RelativeLayout
    android:id="@+id/login_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/register_button"
        android:paddingLeft="@dimen/padding_xxlarge"
        android:paddingRight="@dimen/padding_xxlarge"
        android:src="@drawable/logo"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="@dimen/element_xlarge"
        android:text="@string/action_register"
        android:id="@+id/register_button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

    <LinearLayout
        android:id="@+id/email_login_form"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_above="@+id/register_button"
        android:layout_marginBottom="-40dp">

        <AutoCompleteTextView
            android:id="@+id/net_id"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/element_small"
            android:hint="@string/prompt_username"
            android:inputType="textAutoComplete"
            android:maxLines="1"
            android:singleLine="true"
            android:layout_marginLeft="@dimen/padding_xlarge"
            android:layout_marginRight="@dimen/padding_xlarge"
            android:layout_marginBottom="@dimen/padding_small" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="@dimen/element_small"
            android:hint="@string/prompt_password"
            android:imeActionId="@+id/login"
            android:imeActionLabel="@string/action_sign_in_short"
            android:imeOptions="actionUnspecified"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true"
            android:layout_marginLeft="@dimen/padding_xlarge"
            android:layout_marginRight="@dimen/padding_xlarge" />

        <Button
            android:id="@+id/sign_in_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_xlarge"
            android:layout_marginRight="@dimen/padding_xlarge"
            android:text="@string/action_sign_in" />

    </LinearLayout>

图片: https://drive.google.com/a/cornell.edu/file/d/0BzEvKm6_q_oqbllRd284dS1ubVk/view?usp=sharing

我也尝试在我的 onCreate() 函数中使用 bringToFront() 方法:

findViewById(R.id.email_login_form).bringToFront();

但这没有任何效果。我做错了什么,如何让我的 LinearLayout 出现在顶部?

--更新--

在测试了评论者的一些建议后,我发现这种非常奇怪的行为可能是问题的根源。当我像这样添加一个测试按钮时:

<RelativeLayout
    android:id="@+id/login_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:clipChildren="false">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="@dimen/element_xlarge"
        android:text="@string/action_register"
        android:id="@+id/register_button"
        android:layout_alignParentBottom="true"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/register_button"
        android:paddingLeft="@dimen/padding_xxlarge"
        android:paddingRight="@dimen/padding_xxlarge"
        android:src="@drawable/logo"/>

    <Button
        android:id="@+id/test_button"
        android:text="test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/register_button"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="-30dp"/>

    <LinearLayout
        android:id="@+id/email_login_form"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_above="@id/register_button"
        android:layout_marginBottom="-30dp"
        android:clipChildren="false" >

        <AutoCompleteTextView
            android:id="@+id/net_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/prompt_username"
            android:inputType="textAutoComplete"
            android:maxLines="1"
            android:singleLine="true"
            android:layout_marginLeft="@dimen/padding_xlarge"
            android:layout_marginRight="@dimen/padding_xlarge"
            android:layout_marginBottom="@dimen/padding_small" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/prompt_password"
            android:imeActionId="@+id/login"
            android:imeActionLabel="@string/action_sign_in_short"
            android:imeOptions="actionUnspecified"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true"
            android:layout_marginLeft="@dimen/padding_xlarge"
            android:layout_marginRight="@dimen/padding_xlarge" />

        <Button
            android:id="@+id/sign_in_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_xlarge"
            android:layout_marginRight="@dimen/padding_xlarge"
            android:text="@string/action_sign_in" />

    </LinearLayout>

</RelativeLayout>

然后 test_button 被绘制在 register_button linearLayout 的顶部,尽管它是在 xml 中的 linearLayout 之前声明的。为什么要区别对待测试按钮和LinearLayout?

【问题讨论】:

  • 可以加个截图吗?它可能有助于了解正在发生的事情。 [然后回复,如果你添加了它,我会收到通知]
  • 这个android:layout_above="@+id/register_button" 创建了一个新的ID,而视图还没有被创建。因此,虽然您可以将它用于 ImageView(但您最好先创建 Button),但对于 LinearLayout 是错误的(改用 android:layout_above="@id/register_button")。
  • 已发布截图。 @DerGolem 我做了你建议的改变,但问题仍然存在
  • 抱歉图片分享方式,我不能发布少于10个rep的真实截图(似乎是一个奇怪的规则),所以我能做的最好是google drive。
  • 好吧试试这个,在相对布局中设置“clipchildren=false”和clipchildrenforpadding = false也

标签: android xml android-layout


【解决方案1】:

好的,我创建了类似的 xml,你可以在这里查看: http://pastebin.com/Dx7MXfj5

<Button
   android:id="@+id/registerButton"
   android:layout_width="match_parent"
   android:layout_height="100dp"
   android:layout_alignParentBottom="true"
   android:text="Register" />

<ImageView
   android:id="@+id/logoImageView"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_above="@+id/registerButton"
   android:src="@drawable/ic_confirmation" />

<LinearLayout
   android:id="@+id/loginLayout"
   android:layout_width="220dp"
   android:layout_height="wrap_content"
   android:layout_above="@+id/registerButton"
   android:layout_centerHorizontal="true"
   android:layout_marginBottom="-20dp"
   android:background="#F00"
   android:orientation="vertical" >

    <EditText
       android:id="@+id/editText1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Email" >
    </EditText>

    <EditText
       android:id="@+id/editText2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Password" />

    <Button
       android:id="@+id/button2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Login" />
</LinearLayout>

结果:

【讨论】:

  • 这行得通,谢谢。不过,我仍然对为什么我的原始文件不起作用感到非常困惑。两者似乎没有太大区别。
  • 我不明白为什么原来的布局也不起作用
  • 编辑答案并添加简短摘要来说明您所做的更改以使其正常工作会很有帮助。
猜你喜欢
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多