how to get Theme attributes values

后端 未结 3 463
梦谈多话
梦谈多话 2020-12-29 02:54

Is it possible to obtain styled attributes values from particular Theme without setting the theme up to application/activity? (I mean before invoking context.setTheme(

相关标签:
3条回答
  • 2020-12-29 03:14

    JavaDoc:

    method TypedArray android.content.res.Resources.Theme.obtainStyledAttributes(int[] attrs)

    Return a TypedArray holding the values defined by Theme which are listed in attrs.

    Be sure to call TypedArray.recycle() when you are done with the array.

    0 讨论(0)
  • 2020-12-29 03:30

    For example, to get editTextColor attribute's value of a theme called MyTheme:

    TypedArray a = getTheme().obtainStyledAttributes(
            R.style.MyTheme,
            new int[] { R.attr.editTextColor });
    
    // Get color hex code (eg, #fff)
    int intColor = a.getColor(0 /* index */, 0 /* defaultVal */);
    String hexColor = Integer.toHexString(intColor);
    
    // Don't forget to recycle
    a.recycle();
    
    0 讨论(0)
  • 2020-12-29 03:32

    if you need it in the xml file, you can use something like this:

    style="?android:attr/panelTextAppearance"
    

    for example:

    <TextView
        style="?android:attr/panelTextAppearance"
        android:paddingTop="?android:attr/paddingTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/your_text"
        />
    

    if you're using eclipse, control+click on the item, to see other possible values (a file attrs.xml will open).

    0 讨论(0)
提交回复
热议问题