简体   繁体   中英

android: update views position and size

I want to update position and size of view programmatically, but I am unable to do this, if i work on one of these two, it works fine. First I update the 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);

Now this code starts for updating the position

    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);

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