简体   繁体   中英

programmatically bottom margin to ImageView not working

marginBottom not working. It works but the margin is only a few space regardless of how much margin is applied. Why?

Java-code :

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>

Have you notice that in setMargins method parameters are in pixel and not in dp? take a look at android doc page

So depending of your device screen size but 20 px can be very small.

Don't forget to call requestLayout() to take in account margin changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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