Android 动态更改RelativeLayout的边距,该边距通过xml膨胀
我试图通过扩展xml动态添加十个relativeLayouts,但是相对布局重叠了。如何通过代码动态更改膨胀的xml的边距 下面是代码reuse.xmlAndroid 动态更改RelativeLayout的边距,该边距通过xml膨胀,android,android-relativelayout,Android,Android Relativelayout,我试图通过扩展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_w
<?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");
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.item);
newsLayout=(RelativeLayout)findViewById(R.id.newsLayout);
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
RelativeLayout行[]=新的RelativeLayout[10];
TextView text[]=新的TextView[10];
对于(int i=0;i您可以使用LinearLayout
而不是RelativeLayout
对于新闻版面
并使方向垂直
而不是newsLayout=(RelativeLayout)findViewById(R.id.newsLayout);
写入:newsLayout=(LinearLayout)findViewById(R.id.newsLayout);
剩下的代码将保持原样
这将解决您的问题。再问一个问题,现在我有10个线性布局,单击每个线性布局它将执行一些操作。如何识别每个线性布局。当我设置ClickListener时,所有线性布局都具有相同的ID。您的帮助将非常有用helpful@Badrinath当前位置在这方面我一定会帮你的。但是据鲁请访问此网站,您不能在评论中提出新问题。请提出新问题。我现在有空。我将尝试回答。谢谢。