2017-08-10 38 views
-2
public static int getThemeColor(Context context, int attribute, int defaultColor) { 
    int themeColor = 0; 
    String packageName = context.getPackageName(); 
    try { 
     Context packageContext = context.createPackageContext(packageName, 0); 
     ApplicationInfo applicationInfo = 
      context.getPackageManager().getApplicationInfo(packageName, 0); 
     packageContext.setTheme(applicationInfo.theme); 
     Resources.Theme theme = packageContext.getTheme(); 
     TypedArray ta = theme.obtainStyledAttributes(new int[] {attribute}); 
     themeColor = ta.getColor(0, defaultColor); 
     ta.recycle(); 
    } catch (PackageManager.NameNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return themeColor; 
} 

我的問題是關於這一行:什麼是 「新INT [] {SOME_VALUE}」 的Java語法意味着

TypedArray ta = theme.obtainStyledAttributes(new int[] {attribute}); 

new int[] {attribute}

+2

它定義了int數組,其屬性是其唯一元素 – Smit

回答

0
new int[] {attribute} 

這初始化具有一個元素且值設置爲attribute的原始整數陣列

您也可以使用以下方法初始化一個原始的整數數組:

int[] intArray = new int[2]; 
int[] intArray = {4,5}; 
int[] intArray = new int[]{4,5}; 

希望這有助於。