Android XML布局搞砸了?

时间:2016-10-11 07:52:44

标签: android xml

XML Link

我正在设计一个带有多个按钮的应用,但它无法正常工作。

这就是它在Android工作室中的显示方式 How it shows in the Android studio

这是它在模拟器中显示的方式 enter image description here

5 个答案:

答案 0 :(得分:0)

可能的原因可能是您有多种布局文件用于各种屏幕密度/屏幕尺寸/ Android版本。

检查布局文件夹并确保多个文件夹中没有多个lauout文件;)

答案 1 :(得分:0)

查看您的XML定义:

    <Button
        android:text="Back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:layout_toLeftOf="@+id/button2"
        android:layout_toStartOf="@+id/progressBar2"
        android:layout_marginRight="11dp"
        android:layout_marginEnd="11dp"
        android:layout_alignBottom="@+id/button2" />

    <Button
        android:text="Sign up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:id="@+id/button2"
        android:layout_below="@+id/editText2"
        android:layout_toRightOf="@+id/button5"
        android:layout_toEndOf="@+id/progressBar2"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp" />

您的基础是&#34;注册&#34;按钮到&#34;返回&#34;按钮说明:

    android:layout_toRightOf="@+id/button5"

我将假设您使用button5隐藏View.GONE,而button5实际上会阻止button5被提取。< / p>

现在布局中缺少button2View.INVISIBLE将不再有依赖的基础。从而使它一直向左移动。

解决方案非常简单:使用View.GONE而非 android:visibility="invisible" 来隐藏与其他视图相关的视图:

  • 如果您通过XML隐藏它:

        button5.setVisibility(View.INVISIBLE);
    
  • 如果您通过Java以编程方式隐藏它:

    leftDiv

答案 2 :(得分:0)

xml布局中有circular dependancy

你不能/不应该这样做:

<Button
        android:id="@+id/button5"
        android:layout_toLeftOf="@+id/button2" />
<Button
        android:id="@+id/button2"
        android:layout_toRightOf="@+id/button5" />

仅基于父项(例如alignParentLeft="true")设置其中一个的位置,然后您可以根据第一个按钮设置另一个的位置。 Circular dependancies有意外结果,可能会在StudioAndroidAndroid的不同版本中显示不同的结果。

答案 3 :(得分:0)

我不确定为什么android studio中的显示是正确的,但是快速浏览一下你的xml,你可以看到back buttonsign up button的引用搞砸了。请记住,相对布局使用view A的概念,您可以将view B用作<Button android:text="Back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button5" android:layout_toLeftOf="@+id/button2" android:layout_toStartOf="@+id/progressBar2" android:layout_marginRight="11dp" android:layout_marginEnd="11dp" android:layout_alignBottom="@+id/button2" /> <Button android:text="Sign up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:id="@+id/button2" android:layout_below="@+id/editText2" android:layout_toRightOf="@+id/button5" android:layout_toEndOf="@+id/progressBar2" android:layout_marginLeft="14dp" android:layout_marginStart="14dp" /> 位置的参考,在您的xml中:

button5

button2引用button2,反之亦然,其中一个问题是您甚至不知道button5的位置,但您将其用作参考。此外,button2取决于button5的位置,但您使用button2作为editText2的参考。

由于editText2确定了自己的位置,我建议您使用android:layout_below="@+id/editText2"作为两个按钮的参考。使用android:layout_alignParentLeft="true" android:layout_alignParentStart="true" ,然后根据您的需要添加上边距。之后,使用

android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"

对于后退按钮,请使用

SN:123321123
PCName:blabla
Something.else:Text

用于注册按钮。然后你可以调整两个按钮的余量。

答案 4 :(得分:0)

您的xml布局中具有循环依赖性。

你可以看到

试试这个

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:layout_marginTop="39dp"
    android:id="@+id/editText"
    android:hint="email"
    android:textAlignment="center"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:layout_marginTop="47dp"
    android:id="@+id/editText2"
    android:hint="pass"
    android:textAlignment="center"
    android:layout_below="@+id/editText"
    android:layout_alignLeft="@+id/editText"
    android:layout_alignStart="@+id/editText" />

<Button
    android:text="Back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button5"
    android:layout_toStartOf="@+id/progressBar2"
    android:layout_marginRight="11dp"
    android:layout_marginEnd="11dp"
    android:layout_alignBottom="@+id/button2"
    android:layout_toLeftOf="@+id/progressBar2" />

<Button
    android:text="Sign up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:id="@+id/button2"
    android:layout_below="@+id/editText2"
    android:layout_toEndOf="@+id/progressBar2"
    android:layout_marginLeft="14dp"
    android:layout_marginStart="14dp"
    android:layout_toRightOf="@+id/progressBar2" />

<ProgressBar
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="85dp"
    android:id="@+id/progressBar2"
    android:layout_below="@+id/button5"
    android:layout_centerHorizontal="true" />
</RelativeLayout>
相关问题