2014-07-16 21 views
0

我的UI中大部分元素都引用了字體大小,大小,顏色等的樣式資源。是否可以動態更改其中一個<item>的值?我希望能夠獲得屏幕的物理尺寸並更改尺寸,使其佔用屏幕尺寸大致相同的屏幕百分比。 <item name="android:textSize">12dp</item>將去<item name="android:textSize">15dp</item>用Java更改Android中的XML樣式資源

+0

您不能在運行時更改'資源'。 – codeMagic

+0

:(有沒有類似於文本大小的重量?我有文本視圖的邊界大小縮放很大,我只是需要文本來實際縮放。 – user1971

回答

0

你的意思是你想要應用不同的屏幕尺寸樣式。

看看這個: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); 
+0

排序,它需要更多地依賴於物理大小(英寸和毫米),而不是dpi或縱橫比。values/styles-small/text_size.xml(normal,large,xlarge)是我在找什麼? – user1971

+0

它不像我想的那樣動態,但它使用從一個普通的基礎樣式繼承的文本大小樣式很好地工作 – user1971

+0

是的,我們仍然不知道如何找到一種方法來動態地改變java代碼,我們必須使用這種方法 –