Android 为什么setMargins不能和我的亲戚一起工作?
我在另一个相对布局的内部有一个相对布局。当我使用内部布局进行线性布局时,代码工作得很好。我在这里读到了很多这样的内容。有时提到使用的布局参数应该是父布局。我不太明白,这是我的错吗 提前谢谢各位 我的XML文件Android 为什么setMargins不能和我的亲戚一起工作?,android,android-layout,Android,Android Layout,我在另一个相对布局的内部有一个相对布局。当我使用内部布局进行线性布局时,代码工作得很好。我在这里读到了很多这样的内容。有时提到使用的布局参数应该是父布局。我不太明白,这是我的错吗 提前谢谢各位 我的XML文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" androi
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rel1" >
<RelativeLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="@drawable/helo" />
</RelativeLayout>
</RelativeLayout>
由于您已将android:layout\u alignParentRight设置为true,leftMargin将不会生效,而将android:layout\u centerVertical设置为true,topMargin也不会生效。是的,工作正常,谢谢。只是复制粘贴代码,这就是为什么会发生的原因。谢谢。
public void showcopter()
{
ImageView image=(ImageView)findViewById(R.id.imageView1);
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)(image.getLayoutParams());
lp.leftMargin =0;//(int)((width-150)*density);
lp.topMargin=(int)(hg-(30*density)); //hg is a global variale which stores the screen height
// Log.d("hello_girl",hg+" ");
// lp.setMargins((int) ((width-150)*density),(int)(hg-(30*density)),0,0);
image.setLayoutParams(lp);
image.requestLayout();
}