下面列出了怎么用androidx.annotation.XmlRes的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Creates a keyboard from the given xml key layout file. Weeds out rows
* that have a keyboard mode defined but don't match the specified mode.
*
* @param context the application or service context
* @param xmlLayoutResId the resource file that contains the keyboard layout and keys.
* @param modeId keyboard mode identifier
* @param width sets width of keyboard
* @param height sets height of keyboard
*/
public Keyboard(@NonNull final Context context,
@XmlRes int xmlLayoutResId, int modeId,
final int width, final int height) {
mDisplayWidth = width;
mDisplayHeight = height;
mDefaultHorizontalGap = 0;
mDefaultWidth = mDisplayWidth / 10;
mDefaultVerticalGap = 0;
mDefaultHeight = mDefaultWidth;
mKeys = new ArrayList<Key>();
mModifierKeys = new ArrayList<Key>();
mKeyboardMode = modeId;
loadKeyboard(context, context.getResources().getXml(xmlLayoutResId));
}
/**
* Creates a keyboard from the given xml key layout file. Weeds out rows
* that have a keyboard mode defined but don't match the specified mode.
*
* @param context the application or service context
* @param xmlLayoutResId the resource file that contains the keyboard layout and keys.
* @param modeId keyboard mode identifier
*/
@SuppressWarnings("SuspiciousNameCombination")
public Keyboard(@NonNull final Context context,
@XmlRes final int xmlLayoutResId, final int modeId) {
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
mDisplayWidth = dm.widthPixels;
mDisplayHeight = dm.heightPixels;
//Log.v(TAG, "keyboard's display metrics:" + dm);
mDefaultHorizontalGap = 0;
mDefaultWidth = mDisplayWidth / 10;
mDefaultVerticalGap = 0;
mDefaultHeight = mDefaultWidth;
mKeys = new ArrayList<Key>();
mModifierKeys = new ArrayList<Key>();
mKeyboardMode = modeId;
loadKeyboard(context, context.getResources().getXml(xmlLayoutResId));
}
public void setResultState(@DrawableRes @XmlRes int imageId) {
if (state == STATE_PROGRESS) {
state = STATE_RESULT;
cancelAllAnimation();
progress.stopAnimation();
image.setImageResource(imageId);
showAnimation = new ShowAnimation(image);
image.startAnimation(showAnimation);
hideAnimation = new HideAnimation(progress);
progress.startAnimation(hideAnimation);
} else {
forceSetResultState(imageId);
}
}
private void forceSetResultState(@DrawableRes @XmlRes int imageId) {
state = STATE_RESULT;
cancelAllAnimation();
setAnimating(false);
image.setImageResource(imageId);
image.setAlpha(1f);
image.setScaleX(1f);
image.setScaleY(1f);
image.setRotation(0);
progress.setAlpha(0f);
progress.setScaleX(1f);
progress.setScaleY(1f);
progress.setRotation(0);
}
/**
* Inflates the given XML resource and replaces the current preference hierarchy (if any) with
* the preference hierarchy rooted at {@code key}.
*
* @param preferencesResId The XML resource ID to inflate.
* @param key The preference key of the {@link PreferenceScreen} to use as the root of the
* preference hierarchy, or null to use the root {@link PreferenceScreen}.
*/
public void setPreferencesFromResource(@XmlRes int preferencesResId, @Nullable String key) {
requirePreferenceManager();
final PreferenceScreen xmlRoot = mPreferenceManager.inflateFromResource(mStyledContext,
preferencesResId, null);
final Preference root;
if (key != null) {
root = xmlRoot.findPreference(key);
if (!(root instanceof PreferenceScreen)) {
throw new IllegalArgumentException("Preference object with key " + key
+ " is not a PreferenceScreen");
}
} else {
root = xmlRoot;
}
setPreferenceScreen((PreferenceScreen) root);
}
public KeyboardDelegater(
@NonNull final EditText editText,
@NonNull final KeyboardView keyboardView,
@XmlRes final int keyboardLayoutRes) {
if (DEBUG) Log.v(TAG, "コンストラクタ:");
mEditText = editText;
mKeyboardView = keyboardView;
mKeyboardLayoutRes = keyboardLayoutRes;
}
@SuppressWarnings("JavaReflectionMemberAccess")
@Override
public void addPreferencesFromResource(@XmlRes int preferencesResId) {
if(!(this instanceof AboutFragment)) {
U.allowReflection();
try {
Context context = U.wrapContext(U.getDisplayContext(getActivity()));
Class.forName("android.preference.PreferenceManager")
.getMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class)
.invoke(getPreferenceManager(), context, preferencesResId, getPreferenceScreen());
} catch (Exception e) { /* Gracefully fail */ }
}
super.addPreferencesFromResource(preferencesResId);
}
@NonNull
public static AttributeSet parseDrawableXml(
@NonNull Context context, @XmlRes int id, @NonNull CharSequence startTag) {
try {
XmlPullParser parser = context.getResources().getXml(id);
int type;
do {
type = parser.next();
} while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT);
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}
if (!TextUtils.equals(parser.getName(), startTag)) {
throw new XmlPullParserException("Must have a <" + startTag + "> start tag");
}
AttributeSet attrs = Xml.asAttributeSet(parser);
return attrs;
} catch (XmlPullParserException | IOException e) {
NotFoundException exception =
new NotFoundException("Can't load badge resource ID #0x" + Integer.toHexString(id));
exception.initCause(e);
throw exception;
}
}
private void testBadgeGravityValueHelper(@XmlRes int xmlId, int expectedValue) {
TypedArray a = getTypedArray(xmlId);
int value = 0;
if (a.hasValue(R.styleable.Badge_badgeGravity)) {
value = a.getInt(R.styleable.Badge_badgeGravity, 0);
}
assertThat(value).isEqualTo(expectedValue);
a.recycle();
}
private TypedArray getTypedArray(@XmlRes int xmlId) {
AttributeSet attrs = DrawableUtils.parseDrawableXml(context, xmlId, "badge");
@StyleRes int style = attrs.getStyleAttribute();
if (style == 0) {
style = R.style.Widget_MaterialComponents_Badge;
}
return context
.getTheme()
.obtainStyledAttributes(attrs, R.styleable.Badge, R.attr.badgeStyle, style);
}
@DrawableRes @XmlRes
private int getLikeIcon(boolean liked) {
if (liked) {
return R.drawable.ic_heart_red;
} else {
return R.drawable.ic_heart_outline_white;
}
}
@DrawableRes @XmlRes
private int getCollectIcon(boolean collected) {
if (collected) {
return R.drawable.ic_item_collected;
} else {
return R.drawable.ic_item_collect;
}
}
/**
* Inflates the given XML resource and adds the preference hierarchy to the current
* preference hierarchy.
*
* @param preferencesResId The XML resource ID to inflate.
*/
public void addPreferencesFromResource(@XmlRes int preferencesResId) {
requirePreferenceManager();
setPreferenceScreen(mPreferenceManager.inflateFromResource(mStyledContext,
preferencesResId, getPreferenceScreen()));
}
/**
* Inflates the given XML resource and replaces the current preference hierarchy (if any) with the
* preference hierarchy rooted at {@code key}.{@link
* PreferenceFragmentCompat#setPreferencesFromResource(int, String)}
*/
public static void setPreferencesFromResource(
PreferenceFragmentCompat preferenceFragment, @XmlRes int preferencesResId, String key) {
// Set preferences to use device-protected storage.
if (BuildVersionUtils.isAtLeastN()) {
preferenceFragment.getPreferenceManager().setStorageDeviceProtected();
}
preferenceFragment.setPreferencesFromResource(preferencesResId, key);
}
/**
* Inflates the given XML resource and adds the preference hierarchy to the current preference
* hierarchy. {@link PreferenceFragmentCompat#addPreferencesFromResource}
*/
public static void addPreferencesFromResource(
PreferenceFragmentCompat preferenceFragment, @XmlRes int preferencesResId) {
// Set preferences to use device-protected storage.
if (BuildVersionUtils.isAtLeastN()) {
preferenceFragment.getPreferenceManager().setStorageDeviceProtected();
}
preferenceFragment.addPreferencesFromResource(preferencesResId);
}
/**
* Inflates the given XML resource and adds the preference hierarchy to the current preference
* hierarchy. {@link PreferenceFragment#addPreferencesFromResource}
*/
public static void addPreferencesFromResource(
PreferenceFragment preferenceFragment, @XmlRes int preferencesResId) {
// Set preferences to use device-protected storage.
if (BuildVersionUtils.isAtLeastN()) {
preferenceFragment.getPreferenceManager().setStorageDeviceProtected();
}
preferenceFragment.addPreferencesFromResource(preferencesResId);
}
public static PreferenceListFragment newInstance(@XmlRes int xmlId) {
Bundle args = new Bundle();
args.putInt(XML_ID, xmlId);
PreferenceListFragment prefListFragment = new PreferenceListFragment();
prefListFragment.setArguments(args);
return prefListFragment;
}
/**
* Adds a new file to the index
* @param resId The preference file to index
*/
public SearchIndexItem index(@XmlRes int resId) {
SearchIndexItem item = new SearchIndexItem(resId, this);
itemsToIndex.add(item);
return item;
}
/**
* Includes the given R.xml resource in the index
* @param resId The resource to index
*/
private SearchIndexItem(@XmlRes int resId, SearchConfiguration searchConfiguration) {
this.resId = resId;
this.searchConfiguration = searchConfiguration;
}
@XmlRes int getResId() {
return resId;
}
public void setInputExtras(@XmlRes int xmlResId) throws XmlPullParserException, IOException {
XmlResourceParser parser = getResources().getXml(xmlResId);
createInputContentTypeIfNeeded();
mInputContentType.extras = new Bundle();
getResources().parseBundleExtras(parser, mInputContentType.extras);
}
@DrawableRes @XmlRes
private int getDownloadIcon() {
return R.drawable.ic_download_white;
}
public XmlIndexableFragment(
Class<? extends BaseSearchablePolicyPreferenceFragment> fragmentClass,
@XmlRes int xmlRes) {
super(fragmentClass);
this.xmlRes = xmlRes;
}
Screen(@StringRes int t, @XmlRes int p) {
title = t;
preferenceXML = p;
}
/**
* this should return the R.xml.menu file of the preferenceActivity
*/
protected abstract
@XmlRes
int getPreferencesXmlId();
/**
* Returns a BadgeDrawable from the given XML resource. All attributes from {@link
* R.styleable#Badge} and a custom <code>style</code> attribute are supported. A badge resource
* may look like:
*
* <pre>{@code
* <badge
* xmlns:app="http://schemas.android.com/apk/res-auto"
* style="@style/Widget.MaterialComponents.Badge"
* app:maxCharacterCount="2"/>
* }</pre>
*/
@NonNull
public static BadgeDrawable createFromResource(@NonNull Context context, @XmlRes int id) {
AttributeSet attrs = DrawableUtils.parseDrawableXml(context, id, "badge");
@StyleRes int style = attrs.getStyleAttribute();
if (style == 0) {
style = DEFAULT_STYLE;
}
return createFromAttributes(context, attrs, DEFAULT_THEME_ATTR, style);
}
/**
* Returns a ChipDrawable from the given XML resource. All attributes from {@link
* R.styleable#Chip} and a custom <code>style</code> attribute are supported. A chip resource may
* look like:
*
* <pre>{@code
* <chip
* xmlns:app="http://schemas.android.com/apk/res-auto"
* style="@style/Widget.MaterialComponents.Chip.Entry"
* app:chipIcon="@drawable/custom_icon"/>
* }</pre>
*/
@NonNull
public static ChipDrawable createFromResource(@NonNull Context context, @XmlRes int id) {
AttributeSet attrs = DrawableUtils.parseDrawableXml(context, id, "chip");
@StyleRes int style = attrs.getStyleAttribute();
if (style == 0) {
style = R.style.Widget_MaterialComponents_Chip_Entry;
}
return createFromAttributes(context, attrs, R.attr.chipStandaloneStyle, style);
}
/**
* Sets default configs using an XML resource.
*
* @param resourceId Id for the XML resource, which should be in your application's {@code
* res/xml} folder.
* @deprecated Use {@link #setDefaultsAsync} instead.
*/
@Deprecated
public void setDefaults(@XmlRes int resourceId) {
Map<String, String> xmlDefaults = DefaultsXmlParser.getDefaultsFromXml(context, resourceId);
setDefaultsWithStringsMap(xmlDefaults);
}
/**
* Sets default configs using an XML resource.
*
* @param resourceId Id for the XML resource, which should be in your application's {@code
* res/xml} folder.
*/
@NonNull
public Task<Void> setDefaultsAsync(@XmlRes int resourceId) {
Map<String, String> xmlDefaults = DefaultsXmlParser.getDefaultsFromXml(context, resourceId);
return setDefaultsWithStringsMapAsync(xmlDefaults);
}
/**
* Creates a keyboard from the given xml key layout file.
*
* @param context the application or service context
* @param xmlLayoutResId the resource file that contains the keyboard layout and keys.
*/
public Keyboard(@NonNull final Context context, @XmlRes final int xmlLayoutResId) {
this(context, xmlLayoutResId, 0);
}