2010-01-19 82 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

相关问题