繁体   English   中英

在android中以编程方式设置保证金?

[英]Set programmatically margin in android?

我有吼叫xml 在这个xml我需要以MarginTop "@+id/imgFooter" programaticallyMarginTop "@+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);
    }
}

嗯好吧 您应该使用MarginLayoutParams并设置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);

用这个:

lpimgFooter.addRule(RelativeLayout.BELOW, R.id.imgCenter);

暂无
暂无

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

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