簡體   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