动态更改通过xml膨胀的RelativeLayout的边距

时间:2012-09-03 06:59:24

标签: android relativelayout

我试着通过膨胀xml动态添加10个relativeLayouts,但相对布局重叠。如何通过代码动态地改变膨胀xml的边距。

下面是代码reuse.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/aaa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
     >

    <ImageView
        android:id="@+id/image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="5dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ballaya" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="12dp"
        android:layout_weight="1"
        android:text="Srimannarayana earns Rs 8.65 cr on day 1"
        android:textColor="#FFFFFF"
        android:textSize="11sp"
        android:textStyle="bold" />

    <View
        android:id="@+id/seperator"
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:layout_below="@+id/image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:background="#FFFFFF" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:src="@drawable/popover_bg_rghtarw" />

</RelativeLayout>

item.xml的代码

<?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="fill_parent"
    android:background="#06516E" >

    <RelativeLayout 
     android:layout_width="fill_parent"
    android:layout_height="20dp" 
    android:background="#49C7F9">
         <TextView
            android:id="@+id/HeadLines"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:text="Latest News"
            android:textColor="#FFFFFF"
            android:textSize="12sp"
            android:textStyle="bold" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/newsLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="30dp" >

    </RelativeLayout>
</RelativeLayout>

Java代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        setContentView(R.layout.item);

        newsLayout =(RelativeLayout)findViewById(R.id.newsLayout);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout row[] = new RelativeLayout[10];
         TextView text[]= new TextView[10];
        for(int i=0;i<10;i++){

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

       row[i] = (RelativeLayout) inflater.inflate(R.layout.reuse,null);
       params.setMargins(0, 50, 0, 0);
        newsLayout.addView(row[i],params);

         text[i]= (TextView) row[i].findViewById(R.id.text);
        text[i].setText("AAAAAAA");


        }

1 个答案:

答案 0 :(得分:1)

您可以将LinearLayout代替RelativeLayout用于newsLayout,并将方向设为VERTICAL。

而不是newsLayout =(RelativeLayout)findViewById(R.id.newsLayout); 写:newsLayout =(LinearLayout)findViewById(R.id.newsLayout);

其余代码将保持不变。

这将解决您的问题。