以编程方式底部边距到ImageView无法正常工作

时间:2014-09-23 07:10:54

标签: android android-imageview margin

marginBottom无效。它可以工作,但无论应用多少保证金,保证金只有几个空间。为什么呢?

Java代码:

LinearLayout onepage= (LinearLayout) findViewById(R.id.onepage);

RelativeLayout bluegreen = new RelativeLayout(this);
p_ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
bluegreen.setLayoutParams(p_ll);

//some other views that make up the whole page

//bottom most image
ImageButton migs = new ImageButton(this);
p_rl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p_rl.addRule(RelativeLayout.CENTER_HORIZONTAL);
p_rl.setMargins(0, 0, 0, 20);
register.setLayoutParams(p_rl);
register.setImageResource(R.drawable.migs);
register.setPadding(0, 0, 0, 0);

bluegreen.addView(migs);

onepage.addView(bluegreen);

XML:

<LinearLayout
    android:id="@+id/onepage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"        
    android:orientation="vertical"
    tools:context=".PageActivity" >
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您是否注意到在setMargins中,方法参数是以像素为单位而不是在dp中?看看android doc page

因此,根据您的设备屏幕尺寸,但20 px可能非常小。

不要忘记调用requestLayout()来考虑保证金更改。