简体   繁体   English

使用Java在Android中更改XML样式资源

[英]Changing XML style resource in Android with Java

I have most of my elements in the UI referencing a style resource for font size, size, color, etc. Would it be possible to change the value of one of these <item> 's dynamically? 我的UI中的大多数元素都引用字体,大小,颜色等样式资源。是否可以动态更改这些<item>之一的值? I was hoping to get the physical dimensions of the screen and change the size so that it takes up roughly the same percentage of the screen with different screen sizes. 我希望获得屏幕的物理尺寸并更改尺寸,以使其在不同屏幕尺寸下大致占据相同百分比的屏幕。 <item name="android:textSize">12dp</item> would go to <item name="android:textSize">15dp</item> <item name="android:textSize">12dp</item>将转到<item name="android:textSize">15dp</item>

you mean that you want to apply styles for different screen sizes. 您的意思是您想为不同的屏幕尺寸应用样式。

look this: http://developer.android.com/guide/practices/screens_support.html 看起来像这样: http : //developer.android.com/guide/practices/screens_support.html

that will help you. 那会帮助你。

Edit: After a few researches, I've found a possible answer so far. 编辑:经过一些研究,到目前为止,我已经找到了可能的答案。

// declaring resources
int mHeight;
int mWidth;
/* I am considering a TextView, if you want,
 * you can apply it for any widget you like. */
TextView mTextView = (TextView)findViewById(R.id.your_textview_id);

/* right. now we want to get the screen dimensions. */
Display mDisplay = getWindowManager().getDefaultDisplay();
mHeight = mDisplay.getHeight();
mWidth = mDisplay.getWidth();

/* now we have the values that needed.
 * the next thing to do is mathematical operation.
 * do this as you wish. the thing I do is obtain a
 * value that the mHeight is subtracted by mWidth and divided by 2. */
float mTextSize = (float) (mHeight - mWidth) / 2;

// apply it as text size of the TextView.
mTextView.setTextSize(mTextSize);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM