Android 如何使用此代码将文本视图置于左侧,将单选按钮置于右侧?

Android 如何使用此代码将文本视图置于左侧,将单选按钮置于右侧?,android,Android,我有以下xml布局。我试图将文本视图放在左边,将单选按钮放在右边。我怎样才能做到这一点 <?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">

我有以下xml布局。我试图将文本视图放在左边,将单选按钮放在右边。我怎样才能做到这一点

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

谢谢


patrick

您需要将android:layout\u weight=“1”添加到您的文本视图:

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

您需要将android:layout\u weight=“1”添加到您的文本视图:

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

忘记线性布局,使用RelativeLayout。这应该把文本框放在左边,无线电按钮放在右边,就像你问的那样

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


Eclipsed4uto是100%正确的,您应该使用
dp
而不是
sp
,因为它们在所有设备上都会呈现相同的视觉大小。

忘记线性布局,使用RelativeLayout。这应该把文本框放在左边,无线电按钮放在右边,就像你问的那样

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


Eclipsed4uto是100%正确的,您应该使用
dp
而不是
sp
,因为它们在所有设备上都会呈现相同的视觉大小。

这应该将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>

这应将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>


谢谢大家的回答

CaseyB-不知道为什么你的解决方案对我不起作用。以下是您建议的显示输出:

这是一个似乎适合我的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>

这是模拟器的显示输出


Eclipsed4uto-dp而不是sp…谢谢大家的回答

CaseyB-不知道为什么你的解决方案对我不起作用。以下是您建议的显示输出:

这是一个似乎适合我的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>

这是模拟器的显示输出


Eclipsed4utoo-dp而不是sp…谢谢

抱歉,xml没有正确发布。我将尝试使用android:layout\u width=“wrap\u content”为textview修复…修复。不行。单选按钮仍然位于文本视图旁边。使用fill\u parent alsosorry也一样,xml没有正确发布。我将尝试修复…使用android:layout\u width=“wrap\u content”修复textview.no go。单选按钮仍然位于文本视图旁边。使用fill_parent也可以实现同样的效果。当您试图让视图显示在彼此旁边时,应该使用
RelativeLayout
LinearLayout
将它们置于彼此的上方/下方。使用
layout\u margin
属性移动视图。也可以使用
dip
而不是像素。仍然不能正确退出。首先,toLeftOf的值不会编译。我尝试使用“@+id/txtVehName”值。还有,既然我想把收音机按钮放在右边,我就不应该用toRightOf了。我两个都试过了,但是单选按钮都没有显示。你不需要+在那里,这将产生一个新的ID,你需要引用旧的ID。关于layout_toLeftOf值的输入错误已在上面修复。
RelativeLayout
是您在尝试使视图显示在彼此旁边时应该使用的。
LinearLayout
将它们置于彼此的上方/下方。使用
layout\u margin
属性移动视图。也可以使用
dip
而不是像素。仍然不能正确退出。首先,toLeftOf的值不会编译。我尝试使用“@+id/txtVehName”值。还有,既然我想把收音机按钮放在右边,我就不应该用toRightOf了。我两个都试过了,但是单选按钮都没有显示。你不需要+在那里,这将产生一个新的ID,你需要引用旧的ID。上面关于layout_toLeftOf值的输入错误已经修复。我将您发布的代码复制到一个新的xml文件中,并将其加载到布局编辑器中,添加了权重=1,它对我有效。我已经在我的版面中使用过好几次了。我将你发布的代码复制到一个新的xml文件中,并将其加载到版面编辑器中,添加了权重=1,它对我很有效。我已经在我的布局中使用过好几次了。谢谢你的建议。这是我所做的很多事情,但是我不得不使用MaGuTopOP=“10DIP”,所以TeTVIEW会显示在中间。使用android:layout\u alignParentBottom=“true”时,文本视图仍然位于顶部。感谢您的建议。这是我所做的很多事情,但是我不得不使用MaGuTopOP=“10DIP”,所以TeTVIEW会显示在中间。使用android:layout\u alignParentBottom=“true”时,文本视图仍然位于顶部。