如何设置android:layout_marginBottom程序?
要在屏幕上拖动图像,我创建了自己的类:如何设置android:layout_marginBottom程序?,android,android-relativelayout,android-layoutparams,Android,Android Relativelayout,Android Layoutparams,要在屏幕上拖动图像,我创建了自己的类: private class CustomImageView extends AppCompatImageView 在构造函数中,我要设置参数: public CustomImageView(Context context) { super(context); params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, Relative
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);
}
my 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=newrelativelayout.LayoutParams(val1,val2)
,但没关系。
如何设置android:layout_marginBottom在RelativeLayout by code中的设置?尝试使用
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);
}尝试使用
marginlayoutparms
而不是layoutparms
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);
}在相对布局中添加视图时,请尝试使用LayoutParams 像这样的
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.bottomMargin = 50;
relativeLayout.addView(new CutomImageView(context), params);
在相对布局中添加视图时,请尝试使用LayoutParams 像这样的
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.bottomMargin = 50;
relativeLayout.addView(new CutomImageView(context), params);