Android的布局,视图高度等于屏幕大小 [英] Android Layout, view height is equal to screen size

查看:136
本文介绍了Android的布局,视图高度等于屏幕大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中如何制作查看有没有相同的高度,其屏幕尺寸,是否有可能只用XML来实现这一目标?或者如果它必须使用脚本,告诉我如何

in Android how to make a view have same height as its screen size, is it possible to achieve this with only the xml? or if it must use script, tell me how

感谢。

对不起,暂时不明确,感谢您的回复

Sorry for being not clear, and thanks for your reply

但我认为,match_parent和FILL_PARENT属性是不可靠的,因为当我把视图中的一个容器内或更改视图容器层次,它将无法工作。

but i think, match_parent and fill_parent attribute is not reliable, because when i put the view inside one container or change the view container hierarchy, it won't work.

这里的我完整的XML布局。

Here my complete xml layout.

我想要的高度SAM与设备屏幕的元素是相对布局内的最后一个列表视图

The element i want to make the height sam with device screen is the last list view inside relative layout

推荐答案

不,你不能只在XML实现这一目标。

由于Android支持多种屏幕尺寸,在运行时需要检查每个设备,
每个设备的高度可以计算这样

As android support multiple screen sizes, At runtime you need to check for each device , the height for each device can be calculated like this

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;

使用上述code你会得到屏幕的高度,你需要这个高度 DP 设置为你在运行时视图。

With above code you will get the height of screen and you need to set this height in dp to your view at runtime.

为此在你的活动

        // get view you want to resize
        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main); 

        // get layout parameters for that view
        ViewGroup.LayoutParams params = mainLayout.getLayoutParams();

        // change height of the params e.g. 480dp
        params.height = 480;

        // initialize new parameters for my element
        mainLayout.setLayoutParams(new LinearLayout.LayoutParams(params));

这篇关于Android的布局,视图高度等于屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆