Как я могу получить мой TextView слева и Radiobutton справа с этим кодом? - PullRequest
1 голос
/ 19 января 2010

У меня есть следующий макет XML. Я пытаюсь разместить 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>

спасибо

патрик

Ответы [ 4 ]

5 голосов
/ 19 января 2010

Забудьте 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, потому что они будут визуализироватьсяодинаковый размер для всех устройств.

2 голосов
/ 20 января 2010

Спасибо всем за ваши ответы.

CaseyB - не уверен, почему ваше решение не сработает для меня. Вот отображаемый вывод из вашего предложения:

альтернативный текст 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>

здесь вывод на дисплей эмулятора

альтернативный текст http://ikonicsoft.com/img/layout_relative.jpg

Eclipsed4utoo - дп вместо сп ... спасибо

2 голосов
/ 20 января 2010

Это должно поместить 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 голосов
/ 19 января 2010

Вам нужно добавить 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...