2010-08-10 79 views
3

我試圖讓我的TextView擁抱屏幕的左側,而按鈕的擁抱右側(如果可能的話只是爲了強迫症有文本視圖垂直居中以符合按鈕),但不能使用絕對值。這裏是我的編碼:的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="fill_parent" 
> 
<TextView 
android:layout_width="100px" 
android:layout_height="wrap_content" 
android:layout_gravity="left" 
android:text="Change City:" 
/> 
<Button 
android:layout_width="100px" 
android:layout_height="wrap_content" 
android:layout_gravity="right" 
android:text="Click" 
android:id="@+id/citychoicebutton" 
/> 
</LinearLayout> 

我以爲layout_gravity照顧了我想要做的,但顯然不是。

+0

香港專業教育學院發現,如果我使用tablelayout而非LinearLayout中我可以得到TextView的垂直使用的android上的按鈕中心:layout_alignLeft /右,但我仍然無法得到按鈕擁抱屏幕的右側 – dootcher 2010-08-10 05:57:16

回答

3

重力指定如何在元件放置元件本身內,而不是父(畫面)的邊界之內。

這意味着如果你的文本寬度只有50px,但是你已經爲TextView分配了100px,那麼實際的字符會把它們放到100px空間的左邊,中間或右邊(按照重力值)。

使用LinearLayout,您將不得不假設屏幕寬度達到您想要的效果,並使用「dip」作爲單位,並希望獲得最佳效果(在模擬器上以不同的屏幕尺寸對其進行測試)。

更好的選擇是相對佈局,並使用Android:layout_alignParentLeft和android:layout_alignParentRight。

順便提及,它建議使用「浸漬」(密度無關的像素),而不是「PX」用於一般佈局單元。

密度獨立的像素單元是現在「DP」。 「dip」仍然有效。

2

你可以嘗試用

android:layout_alignParentLeft 

爲TextView的和

android:layout_alignParentRight

的按鈕使用RelativeLayout的。要對齊按鈕和TextView,您可以使用

android:layout_alignbaseLIne 

屬性在Button上。