【问题标题】:does textview not like to be placed to left of another widgettextview 不喜欢放在另一个小部件的左侧吗
【发布时间】:2010-10-02 13:47:16
【问题描述】:

这更多是出于好奇,因为我已经解决了我最初的问题。我想要一个按钮位于屏幕的水平中心,左侧有一个文本框,使用相对布局。如果我有以下布局,我很想知道为什么:

    <Button android:id="@+id/pickTime"
    android:layout_width="wrap_content"
    android:layout_below="@id/to_button"
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content"/>

 <TextView android:id="@+id/timetext"
    android:layout_width="wrap_content"
    android:layout_below="@id/to_button"
    android:layout_toLeftOf="@id/pickTime"
    android:layout_alignTop="@id/pickTime"
    android:layout_height="wrap_content"/>

TextView 不显示,但如果我替换:

  android:layout_toLeftOf="@/id/pickTime"

  android:layout_alignParentLeft="true"

它确实显示(尽管左对齐到左边距而不是右对齐到我最初期望的按钮)。这是所有相对布局的问题(即总是必须按设定的顺序定义小部件),还是只是与需要从左侧读取的文本有关?

【问题讨论】:

    标签: android layout textview


    【解决方案1】:

    检查一下,它正在工作:

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <Button android:id="@+id/pickTime"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Button"
        android:layout_height="wrap_content"/>
    
     <TextView android:id="@+id/timetext"
        android:layout_width="wrap_content"
        android:layout_toLeftOf="@id/pickTime"
        android:layout_alignTop="@id/pickTime"
        android:layout_height="wrap_content"
        android:text="TextView"/>
    </RelativeLayout>
    

    我猜你对android:layout_below="@id/to_button" 有疑问,但你没有添加它,我不知道它是什么。 :(

    【讨论】:

    • 我认为这与屏幕的宽度有关。当我在 HVGA 上使用模拟器运行它时,带有 layout_toLeftOf 的原始代码显示正常,但是当我在我的 Hero 或 320 x 480 分辨率上运行它时,它不显示。看起来很奇怪,因为看起来有足够的空间
    • @Stev_k:粘贴整个 xml,以便我们以更有效的方式帮助您。
    • @Marcarse 真的太长了,而且我想我现在知道问题所在了 - 我将相对布局的 layout_width 和 layout_height 作为 wrap_content,不像你。不过感谢您的回答,在我测试您的代码的步骤中,您让我考虑到了这一点,所以您可以获得完整的分数!
    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2011-06-20
    • 1970-01-01
    • 2019-10-06
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多