如何动态地设置高度的EditText [英] How To Dynamically Set EditText Height

查看:332
本文介绍了如何动态地设置高度的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简单地设置一个EditText组件的高度是用户的屏幕高度的50%。

I want to simply set the height of an EditText component to be 50% of the user's screen height.

我创建一个XML文件(main.xml中)的初始布局,在活动的onCreate(捆绑)加载。从XML配置,我知道如何在EditText上的高度设置为一个文本值,例如​​,Android的:在的EditText组件上layout_height =150dip,并从的onCreate(捆绑),我知道我可以调用自动调用setHeight(INT) ,但在调用自动​​调用setHeight(INT)似乎被忽略了,如果我没有在XML中layout_height设置,然后我得到的时候我的应用程序正在启动一个例外,抱怨说高度值是必需的(和应用程序死亡)。

I'm creating the initial layout from an xml file (main.xml), loaded in the Activity's onCreate(Bundle). From the xml configuration, I understand how to set the EditText height to a literal value, e.g., android:layout_height="150dip", and from the onCreate(Bundle), I understand that I can call setHeight(int) on the EditText component, but the call to setHeight(int) appears to be ignored, and if I don't have the layout_height setting in the xml, then I get an exception when my app is starting, complaining that the height value is required (and the app dies).

有没有办法来设置从基于用户的屏幕高度的XML的高度?换句话说,在XML中,是有可能检索用户的屏幕高度,并用它来计算组件的值?

Is there a way to set the height from the xml based on the user's screen height? In other words, within the xml, is it possible to retrieve the user's screen height and use it to calculate a value for a component?

(我放置的EditText与垂直方向的LinearLayout中。)

(I'm placing the EditText in a LinearLayout with vertical orientation.)

推荐答案

使用线性布局,的EditText的权重设置为1,然后添加其他空的观点,并设置其重量为1为好。

Use a linear layout, set the weight of the EditText to 1, then add another empty view and set its weight to 1 as well.

这篇关于如何动态地设置高度的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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