安卓RelativeLayout视图顺序

3

有关从xml文件绘制android视图的绘制顺序已经有几个已回答的问题,我已经阅读了它们并尝试了它们的解决方案,但没有成功。我有一个RelativeLayout,有三个子元素,一个ImageView,一个Button和一个LinearLayout。我希望LinearLayout在顶部,其他两个在后台。根据定义RelativeLayout中视图的Z顺序,兄弟姐妹按照从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中先声明。为什么test_button应该与LinearLayout不同对待?


1
你能添加一张截图吗?这可能有助于理解正在发生的事情。[然后回复我是否已添加,以便我收到通知] - DragonJawad
已发布屏幕截图。@DerGolem,我按照您建议的更改了,但问题仍然存在。 - Platatat
https://docs.google.com/a/cornell.edu/drawings/d/1Izg2AtAsf7NP1JrtcOaKVirJFD2I-HOTRWaU-Gx5ynI/edit?usp=sharing - Platatat
@MoshErsan,我的布局中重要的问题是我需要两个ViewGroups重叠。你提供的布局类似,但我认为它没有任何重叠的元素。 - Platatat
好的,请稍等一下,我会尽力帮助处理这个问题。 - Mohammad Ersan
显示剩余6条评论
1个回答

0

好的,我已经创建了类似的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>

并且结果是:

Xml Result


这个可以用,谢谢。不过我还是很困惑为什么我的原始文件不能用,两个文件看起来差别不大。 - Platatat
我不明白为什么原始布局也无法正常工作。 - Liuting
编辑答案并添加简要总结,说明您所做的更改以使其正常工作,这将非常有帮助。 - user6754053

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接