对于for循环中的值,Relativelayout按钮大小不会更改

时间:2014-09-08 10:28:23

标签: android eclipse

l1是relativelayout按钮宽度和高度没有根据循环代码对齐我不知道为什么plz帮助。

输出图像: enter image description here

   public class MainActivity extends Activity {
        l1=(RelativeLayout)findViewById(R.id.layout1);
        RowOneCollection = new ArrayList<Button>();
                RowOneCollection.add(btn1);
                RowOneCollection.add(btn2);
                RowOneCollection.add(btn3);
                RowOneCollection.add(btn4);

     LayoutWidth=l1.getWidth()/4;
    LayoutHeight=l1.getHeight()/4;
        for(int i=0;i<RowOneCollection.size();i++){
                    RowOneCollection.get(i).setWidth(LayoutWidth);  
                    RowOneCollection.get(i).setHeight(LayoutHeight);
                }
        }

xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout1"
    tools:context="com.example.examplepro.MainActivity" >


 <Button
        android:id="@+id/btn1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

       />

    <Button
        android:id="@+id/btn2"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/btn1"/>

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/btn2"  />
    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_toRightOf="@+id/btn3" />



</RelativeLayout>

  [1]: http://i.stack.imgur.com/uWzOo.png

1 个答案:

答案 0 :(得分:0)

for(int i=0;i<RowOneCollection.size();i++){
               RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) 
 RowOneCollection.get(i).getLayoutParams();

params.height = LayoutHeight;
params.width = LayoutWidth;

 RowOneCollection.get(i).setLayoutParams(params);
            }