Is there a general way to find styles that apply to a view?

How, in general, will you find out which theme attribute to override to change the look of any user interface element?

Currently, I rely on trawling through frame source files: topic definitions in values.xml (usually a support library option), attribute definitions in attrs.xml, and R.styleable class documents.

But this is absolutely hit and miss. This is not only too much time, but sometimes I skip completely, for example, I tried unsuccessfully to learn how to change the text styles in the DatePickerDialog OK and Cancel buttons. Feel free to use this as an example, but if you do, describe your discovery process. The answer I'm looking for is how to find application styles for any user interface element,

Or is there simply no deterministic way to find out? Do you just have to know?

+4
source share
1 answer

Android . , DatePickerDialog Holo Material. , SDK AppCompat. .

, , Hierarchy Viewer, . .

. , DatePickerDialog, :


:

static String getThemeNameFromContext(Context context) {
    Resources.Theme theme = context.getTheme();
    String themeName;
    try {
        Field field = theme.getClass().getDeclaredField("mThemeResId");
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        int themeResId = field.getInt(theme);
        themeName = context.getResources().getResourceEntryName(themeResId);
    } catch (Exception e) {
        // If we are here then the context is most likely the application context.
        // The theme for an application context is always "Theme.DeviceDefault"
        themeName = "Theme.DeviceDefault";
    }
    return themeName;
}

/ :

static String getResourceName(Context context, int attribute) {
    TypedArray typedArray = context.obtainStyledAttributes(new int[]{attribute});
    try {
        int resourceId = typedArray.getResourceId(0, 0);
        return context.getResources().getResourceEntryName(resourceId);
    } finally {
        typedArray.recycle();
    }
}

DatePickerDialog , , :

// Create the DatePickerDialog
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
        new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            }
        }, 2015, Calendar.SEPTEMBER, 9);
// Show the dialog
datePickerDialog.show();

// Get the positive button from the dialog:
Button positiveButton = datePickerDialog.getButton(DatePickerDialog.BUTTON_POSITIVE);
// Get the theme used by this dialog
String theme = getThemeNameFromContext(datePickerDialog.getContext());
// Get the date picker style used by the dialog
String datePickerStyle = getResourceName(datePickerDialog.getContext(), android.R.attr.datePickerStyle);
// Get the style of the positive button:
String buttonStyle = getResourceName(positiveButton.getContext(), android.R.attr.buttonStyle);

Log.i("LOGTAG", "Theme: " + theme);
Log.i("LOGTAG", "datePickerStyle: " + positiveButton);
Log.i("LOGTAG", "buttonStyle: " + buttonStyle);

theme, datePickerStyle buttonStyle:

: ThemeOverlay.Material.Dialog

datePickerStyle: Widget.Material.Light.DatePicker

buttonStyle: Widget.Material.Light.Button


, - . DatePickerDialog, , AlertDialog. , , AlertDialog. , buttonStyle, .

, . , DialogFragment onStart(), :

@Override
public void onStart() {
    super.onStart();
    DatePickerDialog dialog = (DatePickerDialog) getDialog();
    Button btnPos = dialog.getButton(DatePickerDialog.BUTTON_POSITIVE);
    Button btnNeg = dialog.getButton(DatePickerDialog.BUTTON_NEGATIVE);

    /* customize the buttons here */
    btnPos.setText("CUSTOM");
    btnPos.setTextAppearance(android.R.style.TextAppearance_Large);
    btnNeg.setTextColor(Color.RED);
}

enter image description here


:

, , . ** Android. , XML.

+3

Source: https://habr.com/ru/post/1605712/


All Articles