【问题标题】:How set android:layout_marginBottom programm?如何设置 android:layout_marginBottom 程序?
【发布时间】:2017-07-28 07:16:50
【问题描述】:

要在屏幕上拖动图像,我创建了自己的类:

private class CustomImageView extends AppCompatImageView

在构造函数中我要设置参数:

public CustomImageView(Context context) {
    super(context);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    params.bottomMargin = 50;
    this.setScaleType(ImageView.ScaleType.CENTER_CROP);
    this.setLayoutParams(params);
    this.setWillNotDraw(false);
}

我的activity_main.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.eleizo.firstapplication.MainActivity">   
</RelativeLayout>

我想拥有它本来的样子:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.eleizo.firstapplication.MainActivity">

    <ImageView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:layout_marginBottom="50dp"
        />
</RelativeLayout>

为什么字符串"params.bottomMargin = 50" 不起作用?我试图设置params = new RelativeLayout.LayoutParams(val1,val2) 但没关系。 如何通过代码在RelativeLayout中设置android:layout_marginBottom

【问题讨论】:

    标签: android android-relativelayout android-layoutparams


    【解决方案1】:

    在相对布局中添加视图时尝试使用 LayoutParams。 ViewGroup.addView(View, LayoutParams).

    类似的东西。

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    params.bottomMargin = 50;
    
    relativeLayout.addView(new CutomImageView(context), params);
    

    【讨论】:

      【解决方案2】:

      尝试使用MarginLayoutParams 而不是LayoutParams

      if( params instanceof MarginLayoutParams )
      {
          ((MarginLayoutParams) params).bottomMargin = 50;
          ((MarginLayoutParams) params).leftMargin = 10;
      
      }
      

      所以你的代码看起来像

      public CustomImageView(Context context) {
          super(context);
          params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
      
          if( params instanceof MarginLayoutParams )
          {
          ((MarginLayoutParams) params).bottomMargin = 50;
          }
          this.setScaleType(ImageView.ScaleType.CENTER_CROP);
          this.setLayoutParams(params);
          this.setWillNotDraw(false);
      

      }

      【讨论】:

        猜你喜欢
        • 2018-08-17
        • 2013-11-11
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2018-07-03
        • 2011-12-26
        • 2010-11-04
        • 2011-08-29
        相关资源
        最近更新 更多