簡體   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