下面列出了android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void setActionAreaItem(
Context context, ActionAreaItem actionAreaItem, ActionAreaListener listener) {
this.actionAreaItem = actionAreaItem;
TextView textView = findViewById(R.id.text_view);
if (actionAreaItem != null && listener != null) {
textView.setText(actionAreaItem.getContentDescriptionId());
textView.setContentDescription(
getResources().getString(actionAreaItem.getContentDescriptionId()));
updateView(context, USE_DEFAULT_STYLE);
setOnClickListener((View view) -> listener.onClick(actionAreaItem));
} else {
textView.setText("");
textView.setContentDescription("");
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, null, null);
setOnClickListener(null);
}
}
private void updatePreferences(ListView accountList){
for(int i = 0; i < accountList.getChildCount(); i++) {
try {
LinearLayout rootLayout = (LinearLayout) accountList.getChildAt(i);
RelativeLayout preferenceLayout = (RelativeLayout) rootLayout.getChildAt(1);
TextView titleView = (TextView) preferenceLayout.getChildAt(0);
TextView summaryView = (TextView) preferenceLayout.getChildAt(1);
if(titleView.getText().toString().equals(getString(R.string.language))) {
summaryView.setCompoundDrawablePadding(10);
summaryView.setCompoundDrawablesRelativeWithIntrinsicBounds(LanguageUtil.getLanguageFlagDrawable(getActivity(),
LanguageUtil.getCurrentLanguageCode(getActivity())), null, null, null);
} else if(titleView.getText().toString().equals(getString(R.string.theme))) {
summaryView.setText(ThemeUtil.getThemePreview(getActivity(), ThemeUtil.getCurrentTheme(getActivity())));
summaryView.setTextSize(30);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setTextColor(Color.BLACK);
textView.setTextSize(17f);
textView.setPadding(10, 10, 10, 10);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(images.get(position), 0, 0, 0);
textView.setCompoundDrawablePadding(30);
} else {
textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0);
textView.setCompoundDrawablePadding(30);
}
textView.setCompoundDrawablePadding(
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics()));
return view;
}
private void bindView(View view, ListItem item) {
TextView text = (TextView) view;
text.setText(item.label);
if (mShowIcons) {
if (item.icon == null) {
item.icon = mIconResizer.createIconThumbnail(item.resolveInfo.loadIcon(getPackageManager()));
}
text.setCompoundDrawablesRelativeWithIntrinsicBounds(item.icon, null, null, null);
}
}
/**
* @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable,
* Drawable, Drawable, Drawable)
*/
public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
Drawable start, Drawable top, Drawable end, Drawable bottom) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Work around the platform bug described in setCompoundDrawablesRelative() above.
boolean isRtl = isLayoutRtl(textView);
textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
isRtl ? start : end, bottom);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
} else {
textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
}
}
/**
* @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int,
* int)
*/
public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
int start, int top, int end, int bottom) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Work around the platform bug described in setCompoundDrawablesRelative() above.
boolean isRtl = isLayoutRtl(textView);
textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
isRtl ? start : end, bottom);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
} else {
textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
}
}
@Override
public View getView(int position, View view, ViewGroup parent) {
Object menuItem = getItem(position);
if (menuItem instanceof MenuCategory) {
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.view_navigation_drawer_item, parent, false);
}
((TextView) view).setText(((MenuCategory) menuItem).mTitle);
} else {
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.view_navigation_drawer_item, parent, false);
}
TextView itemTitle = (TextView) view;
itemTitle.setText(((MenuActivity) menuItem).mTitle);
//Setting icon menu item
int iconResource = ((MenuActivity) menuItem).mIconResource;
if (iconResource > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
itemTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(iconResource, 0, 0, 0);
} else {
itemTitle.setCompoundDrawablesWithIntrinsicBounds(iconResource, 0, 0, 0);
}
}
}
return view;
}
/**
* @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable,
* Drawable, Drawable, Drawable)
*/
public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
Drawable start, Drawable top, Drawable end, Drawable bottom) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Work around the platform bug described in setCompoundDrawablesRelative() above.
boolean isRtl = isLayoutRtl(textView);
textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
isRtl ? start : end, bottom);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
} else {
textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
}
}
/**
* @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int,
* int)
*/
public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
int start, int top, int end, int bottom) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Work around the platform bug described in setCompoundDrawablesRelative() above.
boolean isRtl = isLayoutRtl(textView);
textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
isRtl ? start : end, bottom);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
} else {
textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
}
}
@NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView tv = v.findViewById(android.R.id.text1);
Item item = getItem(position);
if (item != null) {
Drawable icon = getContext().getDrawable(item.icon);
if (icon != null) {
icon.setTint(getContext().getColor(R.color.colorPrimaryDark));
tv.setCompoundDrawablesRelativeWithIntrinsicBounds(icon, null, null, null);
tv.setCompoundDrawablePadding((int) (16 * mDensity));
}
}
return v;
}
public static void tintAndSetCompoundDrawable (Context context, @DrawableRes
int drawableRes, int color, TextView textview) {
Resources res = context.getResources();
int padding = (int) res.getDimension(R.dimen.activity_horizontal_margin_half);
Drawable drawable = res.getDrawable(drawableRes);
drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
textview.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable,
null, null, null);
textview.setCompoundDrawablePadding(padding);
}
protected void supportVectorDrawable(Context context, TextView view, AttributeSet attrs) {
Drawable[] drawablesCompat = MdVectorDrawableCompat.getFromAttribute(context, attrs,
R.attr.drawableLeftCompat, R.attr.drawableTopCompat, R.attr.drawableRightCompat, R.attr.drawableBottomCompat,
R.attr.drawableStartCompat, R.attr.drawableEndCompat);
boolean shouldInitiate = false;
for (Drawable drawable : drawablesCompat) {
if (drawable != null) {
shouldInitiate = true;
break;
}
}
if (shouldInitiate) {
Drawable[] drawables = view.getCompoundDrawables();
Drawable[] drawablesRelative;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
drawablesRelative = view.getCompoundDrawablesRelative();
} else {
drawablesRelative = drawables;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
view.setCompoundDrawablesRelativeWithIntrinsicBounds(
getDrawable(drawablesCompat[4] != null ? drawablesCompat[4] : drawablesCompat[0], drawablesRelative[0], drawables[0]),
getDrawable(drawablesCompat[1], drawablesRelative[1], drawables[1]),
getDrawable(drawablesCompat[5] != null ? drawablesCompat[5] : drawablesCompat[2], drawablesRelative[2], drawables[2]),
getDrawable(drawablesCompat[3], drawablesRelative[3], drawables[3])
);
} else {
view.setCompoundDrawablesWithIntrinsicBounds(
getDrawable(drawablesCompat[4] != null ? drawablesCompat[4] : drawablesCompat[0], drawablesRelative[0], drawables[0]),
getDrawable(drawablesCompat[1], drawablesRelative[1], drawables[1]),
getDrawable(drawablesCompat[5] != null ? drawablesCompat[5] : drawablesCompat[2], drawablesRelative[2], drawables[2]),
getDrawable(drawablesCompat[3], drawablesRelative[3], drawables[3])
);
}
}
}
public NavigationItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NavigationItem,
defStyleAttr, defStyleRes);
int activityMinSdk = typedArray.getInteger(R.styleable.NavigationItem_minSdk, 26);
// TODO: Remove BuildCompat.isAtLeastP() check when API 28 is finalized.
int deviceMinSdk = BuildCompat.isAtLeastP() ? 28 : Build.VERSION.SDK_INT;
if (deviceMinSdk < activityMinSdk) {
// If device's SDK is lower than the minSdk specified by the NavigationItem, hide it.
setVisibility(View.GONE);
return;
}
String labelText = typedArray.getString(R.styleable.NavigationItem_labelText);
String infoText = typedArray.getString(R.styleable.NavigationItem_infoText);
Drawable logoDrawable = typedArray.getDrawable(R.styleable.NavigationItem_itemLogo);
@ColorRes int colorRes = typedArray.getResourceId(R.styleable.NavigationItem_imageColor, 0);
String launchingActivityName = typedArray.getString(R.styleable.NavigationItem_destinationActivityName);
int imageColor = ContextCompat.getColor(getContext(), colorRes);
typedArray.recycle();
View rootView = LayoutInflater.from(context).inflate(R.layout.navigation_item, this);
TextView buttonLabel = rootView.findViewById(R.id.buttonLabel);
buttonLabel.setText(labelText);
if (logoDrawable != null) {
Drawable mutatedLogoDrawable = logoDrawable.mutate();
mutatedLogoDrawable.setColorFilter(imageColor, PorterDuff.Mode.SRC_IN);
buttonLabel.setCompoundDrawablesRelativeWithIntrinsicBounds(mutatedLogoDrawable, null,
null, null);
}
InfoButton infoButton = rootView.findViewById(R.id.infoButton);
infoButton.setInfoText(infoText);
infoButton.setColorFilter(imageColor);
CardView outerView = rootView.findViewById(R.id.cardView);
outerView.setOnClickListener((view) -> {
if (launchingActivityName != null) {
Intent intent = new Intent();
intent.setClassName(getContext().getPackageName(), launchingActivityName);
context.startActivity(intent);
} else {
Log.w(TAG, "Launching Activity name not set.");
}
});
}
public NavigationItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NavigationItem,
defStyleAttr, defStyleRes);
int activityMinSdk = typedArray.getInteger(R.styleable.NavigationItem_minSdk, 26);
// TODO: Remove BuildCompat.isAtLeastP() check when API 28 is finalized.
int deviceMinSdk = BuildCompat.isAtLeastP() ? 28 : Build.VERSION.SDK_INT;
if (deviceMinSdk < activityMinSdk) {
// If device's SDK is lower than the minSdk specified by the NavigationItem, hide it.
setVisibility(View.GONE);
return;
}
String labelText = typedArray.getString(R.styleable.NavigationItem_labelText);
String infoText = typedArray.getString(R.styleable.NavigationItem_infoText);
Drawable logoDrawable = typedArray.getDrawable(R.styleable.NavigationItem_itemLogo);
@ColorRes int colorRes = typedArray.getResourceId(R.styleable.NavigationItem_imageColor, 0);
String launchingActivityName = typedArray.getString(R.styleable.NavigationItem_destinationActivityName);
int imageColor = ContextCompat.getColor(getContext(), colorRes);
typedArray.recycle();
View rootView = LayoutInflater.from(context).inflate(R.layout.navigation_item, this);
TextView buttonLabel = rootView.findViewById(R.id.buttonLabel);
buttonLabel.setText(labelText);
if (logoDrawable != null) {
Drawable mutatedLogoDrawable = logoDrawable.mutate();
mutatedLogoDrawable.setColorFilter(imageColor, PorterDuff.Mode.SRC_IN);
buttonLabel.setCompoundDrawablesRelativeWithIntrinsicBounds(mutatedLogoDrawable, null,
null, null);
}
InfoButton infoButton = rootView.findViewById(R.id.infoButton);
infoButton.setInfoText(infoText);
infoButton.setColorFilter(imageColor);
CardView outerView = rootView.findViewById(R.id.cardView);
outerView.setOnClickListener((view) -> {
if (launchingActivityName != null) {
Intent intent = new Intent();
intent.setClassName(getContext().getPackageName(), launchingActivityName);
context.startActivity(intent);
} else {
Log.w(TAG, "Launching Activity name not set.");
}
});
}
private ToastNotifier makeCustomNotification(Activity activity, ToastType toastType, String label, String btnLabel, int drawableRes, int style, int duration, boolean undefinedLoad, final ToastListener listener) {
if (duration < 0)
duration = Snackbar.LENGTH_INDEFINITE;
final Snackbar notif = Snackbar.make(activity.findViewById(android.R.id.content), label, duration);
if (style == 0) {
style = R.style.Toast;
}
TypedArray attributes = activity.obtainStyledAttributes(style, R.styleable.ToastAppearance);
int textColor = attributes.getColor(R.styleable.ToastAppearance_toastTextColor, ContextCompat.getColor(activity, R.color.white));
int buttonTextColor = attributes.getColor(R.styleable.ToastAppearance_toastButtonTextColor, ContextCompat.getColor(activity, R.color.pandroid_green_dark));
int backgroundColor = attributes.getColor(R.styleable.ToastAppearance_toastBackground, ContextCompat.getColor(activity, R.color.pandroid_green));
notif.getView().setBackgroundColor(backgroundColor);
((TextView) notif.getView().findViewById(android.support.design.R.id.snackbar_text)).setTextColor(textColor);
TextView actionView = ((TextView) notif.getView().findViewById(android.support.design.R.id.snackbar_action));
actionView.setTextColor(buttonTextColor);
attributes.recycle();
notif.setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);
if (listener != null)
listener.onDismiss();
}
});
Drawable drawable = null;
if (drawableRes > 0) {
drawable = ContextCompat.getDrawable(activity, drawableRes);
}
actionView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, drawable, null);
if (toastType == ToastType.ACTION && btnLabel != null) {
notif.setAction(btnLabel, new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null)
listener.onActionClicked();
}
});
} else if (drawableRes > 0) {
actionView.setVisibility(View.VISIBLE);
actionView.setClickable(false);
actionView.setFocusableInTouchMode(false);
actionView.setFocusable(false);
actionView.setEnabled(false);
}
if (toastType == ToastType.LOADER) {
ProgressWheel progressWheel = new ProgressWheel(activity);
progressWheel.setId(R.id.snakebar_loader);
if (undefinedLoad)
progressWheel.spin();
progressWheel.setBarWidth((int) DeviceUtils.dpToPx(activity, 4));
progressWheel.setCircleRadius((int) DeviceUtils.dpToPx(activity, 30));
progressWheel.setBarColor(buttonTextColor);
progressWheel.setLinearProgress(true);
((Snackbar.SnackbarLayout) notif.getView()).addView(progressWheel, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
notif.show();
lastShowNotif = notif;
return new ToastNotifier() {
@Override
public void setProgress(int progress) {
ProgressWheel loader = (ProgressWheel) notif.getView().findViewById(R.id.snakebar_loader);
if (loader != null) {
loader.setProgress(progress / 100f);
}
}
@Override
public void dismiss() {
notif.dismiss();
}
};
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =
inflater.inflate(
R.layout.fragment_panes_experiment_details, container, false);
emptyView = (TextView) view.findViewById(R.id.empty_list);
emptyView.setText(R.string.empty_experiment);
emptyView.setCompoundDrawablesRelativeWithIntrinsicBounds(
null, null, null, view.getResources().getDrawable(R.drawable.empty_run));
progressBar = (ProgressBar) view.findViewById(R.id.detailsIndeterminateBar);
details = (RecyclerView) view.findViewById(R.id.details_list);
details.setLayoutManager(
new LinearLayoutManager(
view.getContext(), LinearLayoutManager.VERTICAL, /* don't reverse layout */ false));
DetailsAdapter adapter = new DetailsAdapter(this, savedInstanceState);
loadedExperiment.subscribe(
experiment -> {
boolean includeInvalidRuns = false;
adapter.setScalarDisplayOptions(scalarDisplayOptions);
adapter.setData(experiment, experiment.getTrials(includeArchived, includeInvalidRuns));
if (activeTrialId != null) {
adapter.addActiveRecording(experiment.getTrial(activeTrialId));
}
Context context = getContext();
if (context != null) {
setTitle(experiment.getDisplayTitle(context));
}
},
error -> {
if (Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, "loadedExperiment next failed", error);
}
throw new IllegalStateException("loadedExperiment next failed", error);
});
this.adapter = adapter;
details.setAdapter(adapter);
// TODO: Because scalarDisplayOptions are static, if the options are changed during the
// time we are on this page it probably won't have an effect. Since graph options are
// hidden from non-userdebug users, and not shown in the ExperimentDetails menu even when
// enabled, this is OK for now.
scalarDisplayOptions = new ScalarDisplayOptions();
GraphOptionsController graphOptionsController = new GraphOptionsController(getActivity());
graphOptionsController.loadIntoScalarDisplayOptions(scalarDisplayOptions, view);
ActionAreaView actionArea = view.findViewById(R.id.action_area);
ExtendedFloatingActionButton recordButton = view.findViewById(R.id.record);
ExperimentActivity experimentActivity = (ExperimentActivity) getActivity();
if (experimentActivity.isTwoPane()) {
recordButton.setVisibility(View.GONE);
actionArea.setVisibility(View.GONE);
} else {
actionController.attachStopButton(recordButton, getChildFragmentManager());
actionArea.addItems(
getContext(), experimentActivity.getActionAreaItems(), experimentActivity);
actionArea.setUpScrollListener(details);
actionController.attachActionArea(actionArea);
}
if (savedInstanceState != null) {
includeArchived = savedInstanceState.getBoolean(EXTRA_INCLUDE_ARCHIVED, false);
getActivity().invalidateOptionsMenu();
}
return view;
}
/**
* Binds the {@code textView} with the specified {@code contact}.
*
* @param contact The contact.
* @param textView The TextView.
*/
public static void bind(Contact contact, TextView textView) {
textView.setText(contact.getName());
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(contact.getIcon(), 0, 0, 0);
}
/**
* Binds the {@code textView} with the specified {@code contact}.
*
* @param contact The contact.
* @param textView The TextView.
*/
public static void bind(Contact contact, TextView textView) {
textView.setText(contact.getName());
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(contact.getIcon(), 0, 0, 0);
}
/**
* Binds the {@code textView} with the specified {@code contact}.
*
* @param contact The contact.
* @param textView The TextView.
*/
public static void bind(Contact contact, TextView textView) {
textView.setText(contact.getName());
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(contact.getIcon(), 0, 0, 0);
}