2010-10-02 47 views
1

這是比其他任何事情都好奇,因爲我已經解決了我的原始問題。我想要一個按鈕,使用相對佈局,在屏幕的左側有一個文本框。我很好奇,想知道爲什麼,如果我有以下佈局: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" 

它確實顯示(儘管左對齊左邊緣,而不是右對齊我最初期待的按鈕)。這是所有相關佈局的問題嗎(即總是必須按照設定順序定義小部件),還是僅僅需要從左側讀取文本?

回答

0

檢查這一點,它的工作:

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