textview不喜欢被放置到另一个部件的左侧

问题描述:

这是比其他任何事情都好奇,因为我已经解决了我的原始问题。我想要一个按钮,使用相对布局,在屏幕的左侧有一个文本框。我很好奇,想知道为什么,如果我有以下布局:textview不喜欢被放置到另一个部件的左侧

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

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

检查这一点,它的工作:

<?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"但你没有添加它,我不知道它是什么。 :(

+0

我认为这是与屏幕宽度有关当我用HVGA上的模拟器运行它时,我的原始代码与layout_toLeftOf显示正常,但是当我以英雄或320 x 480分辨率运行它时,看起来很奇怪,因为它看起来有足够的空间 – 2010-10-02 14:30:28

+0

@Stev_k:粘贴整个xml,所以我们可以以更有效的方式帮助你。 – Macarse 2010-10-02 14:43:52

+0

@Marcarse真的太长了,除此之外我想我现在知道问题是什么 - 我的布局宽度和布局高度相对布局为wrap_content,不像你。谢谢你的回答,但是当我通过测试你的代码的步骤时,你让我想起了它,所以你可以拥有满分! – 2010-10-02 15:48:47