0

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");


        }
4

1 に答える 1

1

forLinearLayoutの代わりに使用して、向きを VERTICAL にすることができます。RelativeLayoutnewsLayout

そして、newsLayout =(RelativeLayout)findViewById(R.id.newsLayout); 書く代わりに:newsLayout =(LinearLayout)findViewById(R.id.newsLayout);

そして、コードの残りの部分はそのままになります。

これで問題は解決します。

于 2012-09-03T07:04:31.793 に答える