Android筆記:測量控件寬高和動態設置控件寬高

1.測算控件寬高java

 初始化UI時,沒法直接經過getWidth()或getHeight()獲取到控件的寬度或高度。可採用定時測算去獲取控件的寬度/高度,方法以下:linux

//每隔10毫秒測算一次寬度,直到測算完畢
mMeasureAction = new Runnable()
{
    @Override
    public void run()
    {
        if (mLayout_map.getWidth() != 0)
        {
            Log.i("w", mLayout_map.getWidth() + "");
            Log.i("h", mLayout_map.getHeight() + "");
       }
        else
        {
            mLayout_map.postDelayed(mMeasureAction, 10);
        }
    }
};
mLayout_map.postDelayed(mMeasureAction, 10);


參考資料:http://blog.163.com/zhaolin53636848@126/blog/static/490866682012065373798/android



2.動態修改控件的寬高ide

  好比,將p_w_picpathview的寬度改成200,高度改成寬度的3/4。其中RelativeLayout是p_w_picpathview的父佈局,方法以下:佈局

import android.widget.RelativeLayout.LayoutParams;


int imgWeight = 200;
RelativeLayout.LayoutParams sp_params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
sp_params.width = imgWeight;
sp_params.height = imgWeight * 3 /4 ;
p_w_picpathView.setLayoutParams(sp_params);



參考資料:http://www.linuxidc.com/Linux/2011-09/43132p2.htm,post

相關文章
相關標籤/搜索