开发者

How to retrieve layout_height in code

I have a layout that contains several sub-views all which set height like:

<View android:layout_width="fill_parent"
    android:layout_height="18dip"
    android:background="#ff000000" />

How can I retrieve the value stored in layout_height in code?

Ultimately, what I would like to do is being able to adjust the size defined in the layout so I can dynamically change the value defined by layout_height depending on the resolution of the screen resolution and 开发者_JAVA百科the pixel density. Is the only way to do so during the onLayout callback?

It would be much easier to redefined the XML on onCreate.

Comments, advices would be much appreciated.

Thanks JY


Acces the View's Layout and then access the 'height' field: View.getLayoutParams().height


Should have searched a tad further prior to post a question. This does the trick it seems...

DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    factor_y = (dm.ydpi * dm.heightPixels) / (160 * 480);
    factor_x = (dm.xdpi * dm.widthPixels) / (160 * 320);

LinearLayout layout = (LinearLayout) findViewById(R.id.mainlayout);
for (int i = 0; i < layout.getChildCount(); i++)
{
    View v = layout.getChildAt(i);
    int height = v.getLayoutParams().height;
    if (height > 0)
    {
        v.getLayoutParams().height = (int) ((float) height * factor_y);
    }
    int width = v.getLayoutParams().width;
    if (width > 0)
    {
        v.getLayoutParams().width = (int) ((float) width * factor_x);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜