Android的:如何以编程方式设置XML扪变量 [英] Android: How to set XML dimen variables programatically

查看:121
本文介绍了Android的:如何以编程方式设置XML扪变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的布局XML文件中,我通过一个名为 dimens.xml

单独的文件中引用了大量的参数

例如, dimens.xml 包含这样的参数:

 <扪名=textSize_normal> 20dp< /扪>
<扪名=buttonTextSize_normal> 15dp< /扪>
<扪名=editTextSize_normal> 17dp< /扪>
<扪名=buttonHeight_normal> 37dp< /扪>
<扪名=margin_normal> 5DP< /扪>
 

在我的的main.xml ,例如,我将设置文字大小为的TextView 做是这样的:

 安卓TEXTSIZE =@扪/ editTextSize_normal
 

它的伟大工程。

现在,我的问题是,是否有可能设置的值在我的 dimen.xml 扪变量>从我的主要活动文件编程?我所要做的是取屏幕大小,并设定为例如,在 TEXTSIZE 基于屏幕的高度的一小部分,使得它很容易适应于任何屏幕尺寸。我已经一切想通了,我只需要你的帮助来弄清楚如何在我的code设置变量。

解决方案
  

现在,我的问题是,是否有可能以编程方式从我的主要活动设置了地扪变量的值在我dimen.xml文件?

没有。

  

我所要做的是取屏幕大小,以及集,例如,根据屏幕的高度的一小部分的TEXTSIZE使其容易适应任何屏幕大小

忽略,这是一个奇怪的用户界面的方法,做到这一点的方法是删除你的安卓TEXTSIZE 属性和使用改变文字大小在运行时 setTextSize() Java编写的。

  

我有所有的想通了,我只需要你的帮助来弄清楚如何在我的code设置扪变量。

您没有设置扪变量。你申请你计算的小工具,通过制定者如 setTextSize()

In my layout XML files, I reference a lot of parameters through a separate files called dimens.xml.

For example, dimens.xml contains parameters like these:

<dimen name="textSize_normal">20dp</dimen>
<dimen name="buttonTextSize_normal">15dp</dimen>
<dimen name="editTextSize_normal">17dp</dimen>
<dimen name="buttonHeight_normal">37dp</dimen>
<dimen name="margin_normal">5dp</dimen>

And in my main.xml, for example, I would set the text size for a TextView by doing something like this:

android:textSize="@dimen/editTextSize_normal"

It works great.

Now, my questions is, is it possible to set the values for the dimen variables in my dimen.xml file programmatically from my main activity? What I am trying to do is fetch the screen size, and set, for example, the textSize based on a fraction of the height of the screen so that it is easily adaptable to any screen size. I have all that figured out, I just need your help to figure out how to set the dimen variables in my code.

解决方案

Now, my questions is, is it possible to set the values for the dimen variables in my dimen.xml file programmatically from my main activity?

No.

What I am trying to do is fetch the screen size, and set, for example, the textSize based on a fraction of the height of the screen so that it is easily adaptable to any screen size.

Ignoring that this is an odd UI approach, the way to do that is to delete your android:textSize attributes and to change the text size at runtime using setTextSize() in Java.

I have all that figured out, I just need your help to figure out how to set the dimen variables in my code.

You don't "set the dimen variables". You apply your calculations to the widgets, via setters like setTextSize().

这篇关于Android的:如何以编程方式设置XML扪变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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