繁体   English   中英

android:更新视图的位置和大小

[英]android: update views position and size

我想以编程方式更新视图的位置和大小,但是我无法做到这一点,如果我处理这两个视图之一,则效果很好。 首先我更新大小

oldLayout = (android.widget.RelativeLayout.LayoutParams) view.getLayoutParams();
    changeInWidth = oldLayout.width*ratioWidth;
    changeInHeight = oldLayout.height*ratioHeight;


newLayout = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    if (standardWidth>screenWidth)
        newLayout.width = (int) (oldLayout.width-changeInWidth);
    else if (standardWidth<screenWidth)
        newLayout.width = (int) (oldLayout.width+changeInWidth);
    else
        newLayout.width=oldLayout.width;
    if (standardHeight>screenHeight)
        newLayout.height = (int) (oldLayout.height-changeInHeight);
    else if (standardHeight<screenHeight)
        newLayout.height = (int) (oldLayout.height+changeInHeight);
    else
        newLayout.height=oldLayout.height;

    //newLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    //newLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

现在,此代码开始更新位置

    if (standardWidth>screenWidth)
        newLayout.leftMargin =  (int) (oldLayout.leftMargin-changeInWidth);
    else if (standardWidth<screenWidth)
        newLayout.leftMargin =  (int) (oldLayout.leftMargin+changeInWidth);
    else
        newLayout.leftMargin=oldLayout.leftMargin;
    if (standardHeight>screenHeight)
        newLayout.topMargin = (int) (oldLayout.topMargin-changeInHeight);
    else if (standardHeight<screenHeight)
        newLayout.topMargin = (int) (oldLayout.topMargin+changeInHeight);
    else
        newLayout.topMargin=oldLayout.topMargin;

    view.setLayoutParams(newLayout);
        TextView Status = (TextView) findViewById(R.id.Status);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)Status.getLayoutParams();
        Context context = getApplicationContext();
        float d = context.getResources().getDisplayMetrics().density;//get density 
        int p = (int)(80 * d);//size 80 is in pixel so multiply by d to get it in dp
        params.setMargins(0 , p , 0 , 0 ); //substitute parameters for left, top, right, bottom
        Status.setLayoutParams(params);

暂无
暂无

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

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