2014-09-17 4 views
0

У меня есть следующий относительный код макета в моем Android приложение:Совместите изображения рядом с Radiobuttons в RadioGroup

<RadioGroup 
    android:id="@+id/selectSound" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/micLevelLabel" 
    android:checkedButton="@+id/radiohighpitched" 
    android:layout_marginTop="71dp" > 

    <RadioButton 
     android:id="@+id/radiohighpitched" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="High Pitched" 
     android:textColor="@color/android:white" 
     /> 

    <RadioButton 
     android:id="@+id/radiofoghorn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Fog Horn" 
     android:textColor="@color/android:white" 
     /> 

    <RadioButton 
     android:id="@+id/radioalarm" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Alert Tone" 
     android:textColor="@color/android:white" /> 
</RadioGroup> 



<ImageButton 
    android:id="@+id/imageButton2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageButton1" 
    android:layout_below="@+id/imageButton1" 
    android:onClick="playTone2" 
    android:src="@drawable/play_med" /> 

<ImageButton 
    android:id="@+id/imageButton3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageButton2" 
    android:layout_below="@+id/imageButton2" 
    android:onClick="playTone3" 
    android:src="@drawable/play_med" /> 

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/selectSound" 
    android:layout_toRightOf="@+id/micLevelLabel" 
    android:onClick="playTone1" 
    android:src="@drawable/play_med" /> 

Он производит макет, который выглядит следующим образом:

Layout results

Это просто поэтому происходит правильное выравнивание из-за размера изображения ImageButtons. Я недавно переработал приложение, а ImageButtons с правой стороны больше не выстраивается в линию с RadioButtons.

Есть ли более надежный способ построения такого макета, так что ImageButtons всегда выравниваются с соответствующими RadioButtons?

ответ

3

Если это не обязательно, использовать ImageButton, вы можете пойти с этим подходом.

Что я сделал:

Просто использовал drawableight для всех RadioButton следующим образом:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <RadioGroup 
     android:id="@+id/rgroup" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/rd1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:drawableRight="@drawable/ic_launcher" 
      android:text="@string/high" /> 

     <RadioButton 
      android:id="@+id/rd2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:drawableRight="@drawable/ic_launcher" 
      android:text="@string/med" /> 

     <RadioButton 
      android:id="@+id/rd3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:drawableRight="@drawable/ic_launcher" 
      android:text="@string/low" /> 
    </RadioGroup> 

</LinearLayout> 

Выход:

enter image description here

И за клик случае, вы можете получить указание от this question

Надеюсь, это поможет.

Смежные вопросы