2013-06-28 70 views
0

屏幕宽度我有这样的布局:Android的布局:请RadioGroup中占据旁边有一个方形的ImageView

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

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

     <RadioButton ... /> 

     <RadioButton ... /> 
    </RadioGroup> 

    <SquareImageView 
     android:layout_height="match_parent" /> 

</LinearLayout> 

,这里是为SquareImageView代码:

public class SquareImageView extends ImageView { 
    public SquareImageView(Context context) { 
    super(context); 
    } 

    public SquareImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    } 

    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    int size = getMeasuredHeight();//Math.min(getMeasuredWidth(), getMeasuredHeight()); 
    setMeasuredDimension(size, size); 
    } 
} 

现在,我想达到的目标是这样的:

enter image description here

我想RadioGroup中ŧ o占用尽可能多的空间。 LinearLayout的高度应该适应RadioGroup所需的高度。每个RadioButton应该占据RadioGroup的50%。 SquareImageView应该调整它的高度以匹配LinearLayout的高度,并且它的宽度也应该适应使其成为正方形。

这甚至可能吗?

RadioGroup本身没有问题,但棘手的部分是方形ImageView,因为我无法让它自动调整它的宽度。它是方形的,但我需要手动设置它的宽度。如果我没有手动设置宽度,它看起来是正方形的(在Eclipse Preview Layout Manager中),但是它在屏幕之外结束,并且RadioGroup占用整个屏幕宽度。如果我设置了太宽的宽度,那么它的右侧会出现死角。

尝试摆弄各种设置,但没有得到它很正确。我想,以避免在绝对值设定尺寸,以及宁可让RadioGroup中的高度,确定意见维度休息...

回答

0

试试这个:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <RadioGroup 
     android:id="@+id/MyTabs" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weig="1" > 

     <RadioButton 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      ... /> 

     <RadioButton 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      ... /> 
    </RadioGroup> 

    <SquareImageView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" /> 

</LinearLayout> 
0

你可以试试下面的步骤: 1,设置布局如下:

<RadioGroup 
    android:id="@+id/MyTabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1" > 

    <RadioButton 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     ... /> 

    <RadioButton 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     ... /> 
</RadioGroup> 

<SquareImageView 
    android:layout_width="0dp" 
    android:layout_height="match_parent" /> 

2,当视图负载,programatically find the height of linear layout然后squareview该相同值的programatically set the width使其正方形

3,Find the width of the screen然后设置无线电设备组的宽度屏幕宽度和间以上sqaureview宽度之差以便它占据剩余的屏幕宽度。

+0

这几乎可以工作,除了当我将手机旋转到横向模式,然后再返回到纵向模式时,它会中断。第二次旋转后,RadioGroup的宽度不会改变,直到我点击屏幕上的某个项目(例如TextView)。不知道为什么会出现这种情况? – BadCash

相关问题