【问题标题】:The layout of a menu screen gets rearranged and messed up菜单屏幕的布局被重新排列和混乱
【发布时间】:2014-07-13 01:56:45
【问题描述】:

当我运行应用程序并且三个文本视图被文本填充时,文本视图被重新排列并且整个布局被弄乱了。在 sdk 中,布局是应该的,但是一旦应用程序运行并且 textviews 被文本填充,布局就会变得混乱。我认为这与它是一个相对布局有关,但不确定。有人可以帮我解决这个问题吗?谢谢 这是我的 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:background="@drawable/background" >
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.firstapp.Menu$PlaceholderFragment" >


<TextView
    android:id="@+id/txtView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Main Menu"
    android:textAppearance="?android:attr/textAppearanceLarge" />
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView7"
    android:layout_alignBottom="@+id/textView7"
    android:layout_alignParentRight="true"
    android:layout_marginRight="23dp"
    android:text="Logout" />


<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="200dip"
    android:layout_below="@+id/textView5"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="Courses" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/button2"
    android:text="User Type:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/name"
    android:layout_alignLeft="@+id/textView4"
    android:text="Name:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/organization"
    android:layout_alignBottom="@+id/organization"
    android:layout_alignLeft="@+id/textView5"
    android:text="Organization:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/usertype"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignRight="@+id/button2"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/organization"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/usertype"
    android:layout_alignLeft="@+id/usertype"
    android:layout_marginBottom="18dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/organization"
    android:layout_below="@+id/txtView"
    android:layout_marginTop="15dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/button2"
    android:text="LogOut" />

【问题讨论】:

  • 首先,button1 在其声明的开头缺少
  • 在哪里?我在开始时看到
  • 查看您帖子中的代码。使用 'textAppearance="?android:attr/textAppearanceLarge" />' 结束第一个 TextView 代码块后,您的下一行是 'android:id="@+id/button1"'
  • 您在RelativeLayout 定义中做了同样的事情。您已在定义背景的第 5 行末尾关闭了块,因此所有关于填充等的行都将被忽略。

标签: java android xml android-layout


【解决方案1】:

在定义视图之前,您不应尝试在相对关系中使用视图。
例如,您在 textView2 中定义 layout_alignLeft="@+id/textView4" 在定义 textView4 之前。
相对布局有利于构建灵活的布局,但您仍然需要通过仅相对于现有视图定义新视图来以逻辑方式构建它。

相对于根布局定义视图 1。
定义相对于视图 1 或根布局的视图 2。
相对于视图 1、2 或根布局定义视图 3。
等等


补充:
这样想吧。想象一张空桌子。

..我给你一个红色方块,告诉你把它放在桌子中间。
..你这样做。

..我给你一个白色方块,告诉你把它放在红色方块的右边。
..再一次,你这样做了。

..我给你一个绿色方块,告诉你把它放在白色方块上面。
..再一次,没有问题。你这样做。

..我给你一个蓝色块并告诉你把它放在紫色块的左边。
..你说“我不能!!这里没有紫色方块!!”

【讨论】:

  • 正如我在回答中所说,只需确保当您向下滚动浏览 XML 时,您添加的每个视图仅依赖于之前在 XML 布局中已定义的视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
相关资源
最近更新 更多