【问题标题】:Why is setMargins not working with my RelativeLayout?为什么 setMargins 不能与我的 RelativeLayout 一起使用?
【发布时间】:2015-06-30 13:38:44
【问题描述】:

我在另一个相对布局中有一个相对布局。当我使用内部布局作为线性布局时,代码工作正常。我在 SO 上读到了很多这样的questions。有时有人提到使用的布局参数应该属于父布局。我无法完全理解它。这是我出错的地方吗?

提前谢谢大家!

我的 XML 文件

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

我使用边距的功能

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();
     }

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    由于您设置了 android:layout_alignParentRight 为 true,leftMargin 不会生效,而当您设置 android:layout_centerVertical 为 true 时,topMargin 也不会生效。

    【讨论】:

    • 是的,谢谢。只需复制粘贴代码就是它发生的原因。谢谢。
    猜你喜欢
    • 2011-12-25
    • 2020-10-08
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2013-07-02
    • 2015-11-08
    相关资源
    最近更新 更多