简体   繁体   English

Android RelativeLayout查看顺序

[英]Android RelativeLayout View Order

There are several already answered questions regarding the draw order of android views from an xml file, and I have read them and attempted their solutions with no luck. 关于xml文件中android视图的绘制顺序,已经有几个已经回答的问题,我已经阅读了它们,并尝试了自己的解决方案。 I have a RelativeLayout with three children, an ImageView, a Button, and a LinearLayout. 我有一个带有三个孩子的RelativeLayout,一个ImageView,一个Button和一个LinearLayout。 I want the LinearLayout to be on top, with the other two in the background. 我希望LinearLayout位于顶部,其他两个位于背景中。 According to Defining Z order of views of RelativeLayout in Android , siblings are drawn in the order they are read from the xml file, so I placed my LinearLayout last in the file, but it's still drawn in the back. 根据在Android中为RelativeLayout定义Z的视图顺序,按照从xml文件读取的顺序绘制同级,因此我将LinearLayout放置在文件的最后,但仍在背面绘制。 Here is my file: 这是我的文件:

<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>

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

I have also tried using the bringToFront() method in my onCreate() function: 我也尝试在onCreate()函数中使用BringToFront()方法:

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

But this had no effect. 但这没有效果。 What am I doing wrong, and how can I make my LinearLayout appear on top? 我在做什么错,如何使我的LinearLayout出现在顶部?

--UPDATE-- --UPDATE--

After testing several recommendations from commenters, I have found this very weird behavior that might be the root of the problem. 在测试了评论者的一些建议之后,我发现这种非常奇怪的行为可能是问题的根源。 When I add a test Button like so: 当我像这样添加测试按钮时:

<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>

Then the test_button is drawn on top of the register_button and the linearLayout, despite it being declared before the linearLayout in the xml. 然后将test_button绘制在register_button linearLayout的顶部,尽管它是在xml中的linearLayout之前声明的。 Why should the test button be treated differently than the LinearLayout? 为什么应该将测试按钮与LinearLayout区别对待?

Ok, I've created similar xml you can check it here : http://pastebin.com/Dx7MXfj5 好的,我已经创建了类似的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>

and the result: 结果:

Xml结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM