2010-01-19 81 views
1

我有以下佈局xml。我想把TextView放在左邊,Radiobutton放在右邊。我怎樣才能做到這一點?如何使用此代碼將TextView放到左側,並將Radiobutton放到右側?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/txtVehName" 
     android:hint="@string/VEH_NAME" 
     android:textSize="18sp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </TextView> 
    <RadioButton 
     android:id="@+id/rbDefault" 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </RadioButton> 
</LinearLayout> 

感謝

帕特里克

+0

對不起,該XML並沒有正確發佈,我會嘗試修復.. .FIXED – bugzy 2010-01-19 19:25:08

+1

嘗試使用android:layout_width =「wrap_content」作爲textview。 – keyboardP 2010-01-19 19:29:39

+0

不行。單選按鈕仍然位於textview旁邊。它同樣使用fill_parent也是 – bugzy 2010-01-19 19:44:12

回答

2

謝謝大家的回答。

CaseyB - 不知道爲什麼你的解決方案不適合我。這裏是您的建議顯示輸出:

alt text http://ikonicsoft.com/img/layout_weight.jpg

這裏,似乎爲我工作的XML。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/txtVehName" 
     android:hint="@string/VEH_NAME" 
     android:textSize="18dp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     > 
    </TextView> 
    <RadioButton 
     android:id="@+id/rbDefault" 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     > 
    </RadioButton> 
</RelativeLayout> 

這裏是emulater顯示輸出

alt text http://ikonicsoft.com/img/layout_relative.jpg

Eclipsed4utoo - DP,而不是SP ...感謝

-1

您需要的android添加:layout_weight = 「1」 到你的TextView:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/txtVehName" 
     android:hint="@string/VEH_NAME" 
     android:textSize="18sp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 
    </TextView> 
    <RadioButton 
     android:id="@+id/rbDefault" 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    </RadioButton> 
</LinearLayout> 
+0

,這也不起作用。我以爲我已經嘗試過這個。 – bugzy 2010-01-19 19:48:24

+1

我將您發佈的代碼複製到一個新的xml文件中,並將其加載到佈局編輯器中,並添加了weight = 1,它適用於我。我在佈局中多次使用過這個。 – CaseyB 2010-01-19 20:32:57

5

忘記的LinearLayout,使用RelativeLayout的。這應該把TextBox放在左邊,RadioButton放在右邊,就像你問的那樣。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/txtVehName" 
     android:hint="@string/VEH_NAME" 
     android:textSize="18sp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </TextView> 
    <RadioButton 
     android:id="@+id/rbDefault" 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/txtVehName"> 
    </RadioButton> 
</RelativeLayout> 

Eclipsed4utoo是100%正確的,你應該使用的dp代替sp,因爲它們在視覺上就會呈現在所有設備的大小相同。

+1

'RelativeLayout'是你在試圖讓視圖出現在對方旁邊時應該使用的。 「LinearLayout」將它們放在上面/下面。使用'layout_margin'屬性來移動視圖。也可以使用「dip」而不是像素。 – 2010-01-19 20:17:05

+0

仍然沒有放棄權利。首先,toLeftOf的值不會被編譯。我嘗試使用「@ + id/txtVehName」的值。另外,因爲我想在右邊的RadioButton,不應該使用toRightOf。我嘗試了兩種,但RadioButton沒有出現任何一種方式。 – bugzy 2010-01-19 21:13:10

+1

你不想要那裏的那個,這會使一個新的ID,你想參考舊的。 layout_toLeftOf的值錯誤已在上面修正。 – Dinedal 2010-01-19 21:51:26

2

這應該將txtVehName放在左側牆上,而rbDefault放在右側牆上。那是你想要完成的嗎?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/txtVehName" 
     android:hint="@string/VEH_NAME" 
     android:textSize="18sp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true"> 
    </TextView> 
    <RadioButton 
     android:id="@+id/rbDefault" 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true"> 
    </RadioButton> 
</RelativeLayout> 
+0

感謝您的建議。這很重要,但是我不得不使用marginTop =「10dip」,所以textview將顯示在中間。使用android:layout_alignParentBottom =「true」仍然在頂部有textview。 – bugzy 2010-01-19 21:57:31

相關問題