【问题标题】:programmatically bottom margin to ImageView not working以编程方式到 ImageView 的底部边距不起作用
【发布时间】:2014-09-23 16:29:21
【问题描述】:

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>

【问题讨论】:

    标签: android android-imageview margin


    【解决方案1】:

    您是否注意到 setMargins 方法中的参数是像素而不是 dp?看看android doc page

    因此取决于您的设备屏幕尺寸,但 20 像素可能非常小。

    不要忘记调用 requestLayout() 来考虑账户保证金的变化。

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 2017-01-17
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多