在Android中以编程方式设置边距?

机管局

我有吼叫xml在这种情况下,xml我需要MarginTop "@+id/imgFooter" programatically朝着"@+id/imgCenter"。但我不能。我的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rLayout"
    android:background="@drawable/backrepeat">


        <ImageView
            android:id="@+id/imgHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="One"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/header" />

        <ImageView
            android:id="@+id/imgCenter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imgHeader"
            android:layout_above="@+id/imgFooter"
            android:contentDescription="Two"
            android:src="@drawable/center" />


        <ImageView
            android:id="@+id/imgFooter"
            android:contentDescription="Three"
            android:layout_alignParentBottom="true"
            android:adjustViewBounds="true"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/footer" />


</RelativeLayout>

我的MainActivity.class

public class StartPage extends Activity{
    ImageView imgHeader;
    ImageView imgFooter;
    DisplayMetrics metrics;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_page);
                imgHeader = (ImageView) findViewById(R.id.imgHeader);

        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int width = metrics.widthPixels;
        int height = metrics.heightPixels;     

        int MPTopBottomHeader = (98 * width) / 1080;
        int MPLeftRightHeader = (110 * height) / 1920; 


        RelativeLayout.LayoutParams lpimgHeader = new RelativeLayout.LayoutParams(imgHeader.getLayoutParams());
        lpimgHeader.setMargins(MPLeftRightHeader, MPTopBottomHeader, MPLeftRightHeader, MPTopBottomHeader);
        imgHeader.setLayoutParams(lpimgHeader);

        imgFooter = (ImageView) findViewById(R.id.imgFooter);

        int MPTopBottomFooter = (185 * width) / 1080;
        int MPLeftRightFooter = (260 * height) / 1920; 

===== HERE ======>RelativeLayout.LayoutParams lpimgFooter = new RelativeLayout.LayoutParams(imgFooter.getLayoutParams()); 
        lpimgFooter.setMargins(MPLeftRightFooter, MPTopBottomFooter, MPLeftRightFooter, MPTopBottomFooter);
        imgFooter.setLayoutParams(lpimgFooter);
    }
}
用户名

嗯好吧 您应该使用fromMarginLayoutParams和set bottomMargin

/*RelativeLayout.LayoutParams lpimgFooter = new RelativeLayout.LayoutParams(imgFooter.getLayoutParams());
        lpimgFooter.setMargins(MPLeftRightFooter, MPTopBottomFooter, MPLeftRightFooter, MPTopBottomFooter);
        imgFooter.setLayoutParams(lpimgFooter);*/ (Comment this code)
MarginLayoutParams lpimgFooter = (MarginLayoutParams) imgFooter.getLayoutParams();
lpimgFooter.bottomMargin = MPTopBottomFooter;
lpimgFooter.leftMargin = MPLeftRightFooter;
lpimgFooter.rightMargin = MPLeftRightFooter;
imgFooter.setLayoutParams(lpimgFooter);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android以编程方式设置视图边距

来自分类Dev

以编程方式在cardview中设置边距

来自分类Dev

Android:如何以编程方式为片段设置边距?

来自分类Dev

在Crystal Report中以编程方式设置页边距

来自分类Dev

Android-如何在GridLayout中以编程方式设置边距

来自分类Dev

如何在Android中以编程方式为相对布局设置边距?

来自分类Dev

为相对布局android创建并设置以编程方式设置的边距

来自分类Dev

以编程方式设置线性布局之间的边距

来自分类Dev

如何以编程方式设置EditText布局边距?

来自分类Dev

以编程方式在View上设置负边距

来自分类Dev

以编程方式设置线性布局之间的边距

来自分类Dev

Android以编程方式向Webview添加边距

来自分类Dev

使用LayoutParams从FrameLayout中以编程方式删除边距

来自分类Dev

使用LayoutParams从FrameLayout中以编程方式删除边距

来自分类Dev

为相对布局android创建并设置边距编程

来自分类Dev

如何以编程方式相对于 ConstraintLayout 中的另一个视图设置视图的宽度/边距

来自分类Dev

以编程方式无法使用边距布局

来自分类Dev

RelativeLayout以编程方式无法使用边距

来自分类Dev

使用C#以编程方式设置Word文档表单元格边距

来自分类Dev

ListView行在CustomAdapter中以编程方式设置了左边距

来自分类Dev

ListView行在CustomAdapter中以编程方式设置了左边距

来自分类Dev

UITextField以布局方式自动设置边距

来自分类Dev

Android:为AlertDialog框中的编辑文本设置边距

来自分类Dev

Android如何在Relativelayout中设置边距

来自分类Dev

如何以编程方式在 ImageView 中显示多个可绘制对象(带边距)

来自分类Dev

在Android中以编程方式设置布局的大小

来自分类Dev

以编程方式调整图像按钮的边距,高度和宽度

来自分类Dev

以编程方式将边距添加到RelativeLayout

来自分类Dev

以编程方式创建约束以查看控制器边距

Related 相关文章

  1. 1

    Android以编程方式设置视图边距

  2. 2

    以编程方式在cardview中设置边距

  3. 3

    Android:如何以编程方式为片段设置边距?

  4. 4

    在Crystal Report中以编程方式设置页边距

  5. 5

    Android-如何在GridLayout中以编程方式设置边距

  6. 6

    如何在Android中以编程方式为相对布局设置边距?

  7. 7

    为相对布局android创建并设置以编程方式设置的边距

  8. 8

    以编程方式设置线性布局之间的边距

  9. 9

    如何以编程方式设置EditText布局边距?

  10. 10

    以编程方式在View上设置负边距

  11. 11

    以编程方式设置线性布局之间的边距

  12. 12

    Android以编程方式向Webview添加边距

  13. 13

    使用LayoutParams从FrameLayout中以编程方式删除边距

  14. 14

    使用LayoutParams从FrameLayout中以编程方式删除边距

  15. 15

    为相对布局android创建并设置边距编程

  16. 16

    如何以编程方式相对于 ConstraintLayout 中的另一个视图设置视图的宽度/边距

  17. 17

    以编程方式无法使用边距布局

  18. 18

    RelativeLayout以编程方式无法使用边距

  19. 19

    使用C#以编程方式设置Word文档表单元格边距

  20. 20

    ListView行在CustomAdapter中以编程方式设置了左边距

  21. 21

    ListView行在CustomAdapter中以编程方式设置了左边距

  22. 22

    UITextField以布局方式自动设置边距

  23. 23

    Android:为AlertDialog框中的编辑文本设置边距

  24. 24

    Android如何在Relativelayout中设置边距

  25. 25

    如何以编程方式在 ImageView 中显示多个可绘制对象(带边距)

  26. 26

    在Android中以编程方式设置布局的大小

  27. 27

    以编程方式调整图像按钮的边距,高度和宽度

  28. 28

    以编程方式将边距添加到RelativeLayout

  29. 29

    以编程方式创建约束以查看控制器边距

热门标签

归档