繁体   English   中英

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

[英]Changing XML style resource in Android with Java

我的UI中的大多数元素都引用字体,大小,颜色等样式资源。是否可以动态更改这些<item>之一的值? 我希望获得屏幕的物理尺寸并更改尺寸,以使其在不同屏幕尺寸下大致占据相同百分比的屏幕。 <item name="android:textSize">12dp</item>将转到<item name="android:textSize">15dp</item>

您的意思是您想为不同的屏幕尺寸应用样式。

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

那会帮助你。

编辑:经过一些研究,到目前为止,我已经找到了可能的答案。

// 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