2012-09-03 30 views
0

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

下面是代码reuse.xml item.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> 

代码

<?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

可以使用LinearLayout而不是RelativeLayoutnewsLayout,使方向为VERTICAL。和

代替newsLayout =(RelativeLayout)findViewById(R.id.newsLayout); 写:newsLayout =(LinearLayout)findViewById(R.id.newsLayout);

和代码的其余部分将是因为它是。

这将解决您的问题。

+0

它工作得很好!谢谢吨人:) – Badrinath

+0

不客气:) – Shrikant

+0

shrikanth多一个问题,现在我有10个linearlayouts点击每个linearlayout它会执行一些操作。如何识别每个Linearlayout。当我setOnclickListener所有的线性布局都有相同的ID。 Ur的帮助会非常有帮助 – Badrinath