2013-07-20 25 views
0

我很驚訝地發現Android API允許我們通過調用getLayoutParams()方法來調整imageView對象的大小。例如:以「get」開始實際設置屬性的方法是否很常見?

myTimerBar.getLayoutParams().width = timeRemaining; 

沒有相應的setLayoutParams()方法。

具有單詞「get」的方法也允許程序員「設置」屬性的方法很常見嗎?我意識到這種方法在技術上並不是一種「吸氣」方法,但它確實以「get」開頭。

+1

'setLayoutParams()'肯定存在:。http://developer.android.com/ reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams) – CommonsWare

回答

0

getLayoutParams()返回ViewGroup.LayoutParams它有一個實例變量width被聲明爲public,並且在Java中可以訪問公共實例變量並在沒有setter方法的情況下爲其設置新值。

例如:

public class Point 
{ 
    public int x; 
    public int y; 
} 
Point p = new Point(); 
p.x = 10; 
p.y = p.x + 1; 
+0

Aha。這很有道理,謝謝。 –

0

它是常見的有字方法「獲得「還允許 程序員」設置「屬性?我意識到,這種方法不是 技術上「消氣」的方法,但它確實打出「得到啓動。

它不會返回與View相關的LayoutParams對象,該對象不具有可變狀態但View類確實有setLayoutParams(),所以做任何的子類的View您必須創建一個新的LayoutParams設置

相關問題