RelativeLayout没有按预期工作

时间:2013-10-15 03:27:49

标签: android android-layout

我有一个Android布局,其中有一些其他随机的东西,在中间部分我有一个linearLayout,包含4个RelativeLayouts,如下面的XML代码。

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:background="@drawable/backgroudpng">

    </RelativeLayout>
</LinearLayout>

当我尝试将对象置于这种相对布局时,这就是说道。当我在其中放置一个对象时,它将对象放置在布局的另一部分中(而不是将其拖动到的位置!)。例如,如果我将文本框放入相对布局的右上角,它可能会将其放在右下方或中间......

我需要让这个工作或我需要一个不同的方法。我的目标是创建一个自定义按钮,让我可以同时在其上显示三个或四个不同的文本对象。

我正在尝试做什么(如果你也需要我,我可以在gimp中画出来)

______________________________________________________________________________________
|                                                                                     |
|  Some information here                                                              |
|                                               picture of a number from 1-10 here    |
|  Something else about the "button here"                                             |
|                                                                                     |
|_____________________________________________________________________________________|

无论如何我正在考虑使用表面视图来预测这一点,但这似乎有点矫枉过正。基本上我只想创建4个这样的大型可点击界面,它们可以显示一些可以通过点击它们设置的设置。无论如何,如果这是错误的方式,请告诉我!虽然这似乎是一种简单的方法,但这正是我所希望的!

更新:

以下是我正在尝试完成的内容的快速图片:

enter image description here

浅棕色代表背景图像,黑色代表整个视图的背景,然后文本只是示例文本。我希望他们能够像按钮一样点击此视图并更改这些设置背后的设置。我遇到的问题是我将一个小文本对象拖到我想要“类型”设置的位置,当我放开它时,它将被定位在“受影响的行”应该是的位置。

今晚我将尝试下面建议的方法,看看他们是否能解决问题。

到目前为止,感谢您的帮助!

更新2:

这是代码,以及它产生的图片(我已经验证它在我的手机上以及另一部手机和模拟器上看起来像这样)

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/buttonbackground">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView"
        android:textColor="#debabc"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView2"
        android:layout_marginRight="21dp"
        android:textColor="#dec1db"
        android:layout_below="@+id/textView3"
        android:layout_alignParentRight="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView3"
        android:textColor="#debabd"
        android:layout_alignBaseline="@+id/textView"
        android:layout_alignBottom="@+id/textView"
        android:layout_toLeftOf="@+id/textView2"/>
</RelativeLayout>

enter image description here

蓝色框大致是我放置文本的位置,但为了清楚起见,它们在X和Y访问时都重新定位。

此外,这是使用Android Studio,但我在eclipse中检查并加载了布局,它也在做同样的事情。这很奇怪,因为我之前使用过它们并没有遇到过这样的麻烦......我一定是做错了。

其他信息: 尝试使用API​​14,16和18进行渲染。

再次感谢!

2 个答案:

答案 0 :(得分:1)

将相对布局用作根布局,而不是您使用的线性布局。它将使您更容易管理其他布局。

答案 1 :(得分:0)

根本不要使用RelativeLayout。根据您的要求。具有两行的表布局提供了更好的结果。而且它也很容易使用。它不会以不同的屏幕尺寸搞乱您的布局。