简体   繁体   English

以编程方式使ImageView的底部边距不起作用

[英]programmatically bottom margin to ImageView not working

marginBottom not working. marginBottom不起作用。 It works but the margin is only a few space regardless of how much margin is applied. 它可以工作,但无论应用多少边距,边距仅是几个空间。 Why? 为什么?

Java-code : 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: XML:

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

Have you notice that in setMargins method parameters are in pixel and not in dp? 您是否注意到setMargins方法中的参数以像素为单位而不是dp中? take a look at android doc page 看看android doc页面

So depending of your device screen size but 20 px can be very small. 因此,取决于您设备的屏幕尺寸,但是20 px可能很小。

Don't forget to call requestLayout() to take in account margin changes. 不要忘记调用requestLayout()来考虑保证金变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM