我正在尝试通过膨胀 xml 来动态添加十个 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");
}