下面列出了android.widget.TextView#setTextAppearance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressLint("NewApi")
private TextView createTextView(final Element textBox, final int style)
{
final TextView tv = new TextView(context);
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
tv.setTextAppearance(style);
}
else
{
tv.setTextAppearance(context, style);
}
tv.setText(textBox.getAttribute("text"));
return tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int padding = (int) (mDisplayMetrics.density * 10);
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.simple_spinner_item, null);
tv.setText(mItems[position]);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tv.setTextAppearance(SplashActivity.this, R.style.SkinCompatTextAppearance);
tv.setGravity(Gravity.CENTER);
tv.setPadding(padding, padding, padding, padding);
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
return tv;
}
public FloatLabelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context
.obtainStyledAttributes(attrs, R.styleable.FloatLabelLayout);
final int sidePadding = a.getDimensionPixelSize(R.styleable.FloatLabelLayout_floatLabelSidePadding,
dipsToPix(DEFAULT_PADDING_LEFT_RIGHT_DP));
mLabel = new TextView(context);
mLabel.setPadding(sidePadding, 0, sidePadding, 0);
mLabel.setVisibility(INVISIBLE);
mLabel.setTextAppearance(context, a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small));
addView(mLabel, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
a.recycle();
}
public FloatLabelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context
.obtainStyledAttributes(attrs, R.styleable.FloatLabelLayout);
final int sidePadding = a.getDimensionPixelSize(
R.styleable.FloatLabelLayout_floatLabelSidePadding,
dipsToPix(DEFAULT_PADDING_LEFT_RIGHT_DP));
mLabel = new TextView(context);
mLabel.setPadding(sidePadding, 0, sidePadding, 0);
mLabel.setVisibility(INVISIBLE);
mLabel.setTextAppearance(context,
a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small)
);
addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
a.recycle();
}
private void createLabels() {
Context context = new ContextThemeWrapper(getContext(), mLabelsStyle);
for (int i = 0; i < mButtonsCount; i++) {
FloatingActionButton button = (FloatingActionButton) getChildAt(i);
String title = button.getTitle();
if (button == mAddButton || title == null ||
button.getTag(R.id.fab_label) != null) continue;
TextView label = new TextView(context);
label.setTextAppearance(getContext(), mLabelsStyle);
label.setText(button.getTitle());
addView(label);
button.setTag(R.id.fab_label, label);
}
}
protected TextView getTextView(String text, int left, int top, int right, int bottom) {
TextView textView = new TextView(getActivity());
textView.setText(text);
textView.setTextAppearance(getActivity(), R.style.TextView_Label);
textView.setPadding(left, top, right, bottom);
return textView;
}
public FloatingLabelEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context
.obtainStyledAttributes(attrs, R.styleable.FloatingLabelLayout);
final int sidePadding = a.getDimensionPixelSize(
R.styleable.FloatingLabelLayout_floatLabelSidePadding,
dipsToPix(DEFAULT_PADDING_LEFT_RIGHT_DP));
mLabel = new TextView(context);
mLabel.setPadding(sidePadding, 0, sidePadding, 0);
mLabel.setVisibility(INVISIBLE);
mLabel.setTextAppearance(context,
a.getResourceId(R.styleable.FloatingLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small)
);
EditText edit = new EditText(context);
edit.setPadding(sidePadding, 0, sidePadding, 0);
int triggerInt = a.getInt(R.styleable.FloatingLabelLayout_floatLabelTrigger, Trigger.TYPE.getValue());
mTrigger = Trigger.fromValue(triggerInt);
this.addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.addView(edit, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
a.recycle();
}
public FloatLabelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatLabelLayout);
int leftPadding =
a.getDimensionPixelSize(R.styleable.FloatLabelLayout_floatLabelPaddingLeft,
dipsToPix(DEFAULT_LABEL_PADDING_LEFT));
int topPadding = a.getDimensionPixelSize(R.styleable.FloatLabelLayout_floatLabelPaddingTop,
dipsToPix(DEFAULT_LABEL_PADDING_TOP));
int rightPadding =
a.getDimensionPixelSize(R.styleable.FloatLabelLayout_floatLabelPaddingRight,
dipsToPix(DEFAULT_LABEL_PADDING_RIGHT));
int bottomPadding =
a.getDimensionPixelSize(R.styleable.FloatLabelLayout_floatLabelPaddingBottom,
dipsToPix(DEFAULT_LABEL_PADDING_BOTTOM));
mHint = a.getText(R.styleable.FloatLabelLayout_floatLabelHint);
mLabel = new TextView(context);
mLabel.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
mLabel.setVisibility(INVISIBLE);
mLabel.setText(mHint);
ViewCompat.setPivotX(mLabel, 0f);
ViewCompat.setPivotY(mLabel, 0f);
mLabel.setTextAppearance(context,
a.getResourceId(R.styleable.FloatLabelLayout_floatLabelTextAppearance,
android.R.style.TextAppearance_Small));
a.recycle();
addView(mLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mInterpolator = AnimationUtils.loadInterpolator(context,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
? android.R.interpolator.fast_out_slow_in
: android.R.anim.decelerate_interpolator);
}
public ViewHolder(View v) {
super(v);
mTitleTv = (TextView) v.findViewById(android.R.id.text1);
mTitleTv.setTextAppearance(v.getContext(), android.R.style.TextAppearance_DeviceDefault_Small);
mArtistTv = (TextView) v.findViewById(android.R.id.text2);
mArtistTv.setTextAppearance(v.getContext(), android.R.style.TextAppearance_DeviceDefault_Small_Inverse);
}
public VerticalCardView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.com_gabm_fancyplaces_VerticalCardView,
0, 0);
try {
title = a.getString(R.styleable.com_gabm_fancyplaces_VerticalCardView_card_title);
} finally {
a.recycle();
}
// own settings
this.setOrientation(VERTICAL);
this.setPadding(30, 30, 30, 30);
// text view as title
TextView textView = new TextView(context);
textView.setText(title);
textView.setTextAppearance(context, R.style.TextAppearance_AppCompat_Large);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(0, 20, 0, 10);
this.addView(textView);
}
@SuppressWarnings("deprecation")
public static void textAppearanceCompat(TextView textView, int resId) {
if (hasMarshmallow()) {
textView.setTextAppearance(resId);
} else {
textView.setTextAppearance(textView.getContext(), resId);
}
}
public static void setTextAppearance(TextView tv, @StyleRes int textAppearanceRes) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tv.setTextAppearance(textAppearanceRes);
} else {
//noinspection deprecation
tv.setTextAppearance(tv.getContext(), textAppearanceRes);
}
}
@Override
public void bindView(View view, boolean selected) {
super.bindView(view, selected);
Context context = view.getContext();
TextView badge = ViewHolder.get(view, R.id.badge);
int badgeColor = getBadgeColor(context);
String badgeString = getBadge(context);
if (badgeString != null) {
badge.setText(badgeString);
if (badgeColor == 0) {
badge.setBackgroundColor(0);
badge.setTextAppearance(context, R.style.TextAppearance_MaterialNavigationDrawer_Badge_NoBackground);
// int textColorPrimary = Utils.getColor(context, android.R.attr.textColorPrimary, 0xde000000);
// int textColorSecondary = Utils.getColor(context, android.R.attr.textColorSecondary, 0x89000000);
// ColorStateList badgeTextColor = Utils.createActivatedColor(textColorSecondary, textColorPrimary);
// badge.setTextColor(badgeTextColor);
int textColor;
if (selected) {
textColor = Utils.getColor(context, android.R.attr.textColorPrimary, 0xde000000);
} else {
textColor = Utils.getColor(context, android.R.attr.textColorSecondary, 0x89000000);
}
badge.setTextColor(textColor);
} else {
Utils.setBackground(badge, Utils.createRoundRect(context, badgeColor, 1));
badge.setTextAppearance(context, R.style.TextAppearance_MaterialNavigationDrawer_Badge);
badge.setTextColor(Utils.computeTextColor(context, badgeColor));
}
badge.setVisibility(View.VISIBLE);
} else {
badge.setVisibility(View.GONE);
}
}
@SuppressWarnings("deprecation")
private void setTextAppearanceCompat(TextView textView, int appearanceRes) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextAppearance(appearanceRes);
} else {
textView.setTextAppearance(textView.getContext(), appearanceRes);
}
}
private View createView(int position, View convertView) {
View v;
if (convertView == null) {
LinearLayout fileLayout = new LinearLayout(context);
fileLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
fileLayout.setOrientation(LinearLayout.HORIZONTAL);
fileLayout.setGravity(Gravity.CENTER_VERTICAL);
fileLayout.setPadding(5, 10, 0, 10);
ImageView ivFile = new ImageView(context);
ivFile.setTag(1);
ivFile.setLayoutParams(new LayoutParams(48, 48));
TextView tvFile = new TextView(context);
tvFile.setTag(2);
tvFile.setTextColor(android.graphics.Color.WHITE);
tvFile.setTextAppearance(context, android.R.style.TextAppearance_Large);
tvFile.setPadding(5, 5, 0, 0);
tvFile.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
fileLayout.addView(ivFile);
fileLayout.addView(tvFile);
v = fileLayout;
} else {
v = convertView;
}
bindView(position, v);
return v;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
setBackgroundDrawable(mBackground);
mTitleView = (TextView) findViewById(R.id.abs__title);
if (mTextAppearance != -1) {
mTitleView.setTextAppearance(mTextAppearanceContext,
mTextAppearance);
}
mShortcutView = (TextView) findViewById(R.id.abs__shortcut);
}
public Marker(Context context, AttributeSet attrs, int defStyleAttr, String maxValue) {
super(context, attrs, defStyleAttr);
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DiscreteSeekBar,
R.attr.discreteSeekBarStyle, R.style.Widget_DiscreteSeekBar);
int padding = (int) (PADDING_DP * displayMetrics.density) * 2;
int textAppearanceId = a.getResourceId(R.styleable.DiscreteSeekBar_dsb_indicatorTextAppearance,
R.style.Widget_DiscreteIndicatorTextAppearance);
mNumber = new TextView(context);
//Add some padding to this textView so the bubble has some space to breath
mNumber.setPadding(padding, 0, padding, 0);
mNumber.setTextAppearance(context, textAppearanceId);
mNumber.setGravity(Gravity.CENTER);
mNumber.setText(maxValue);
mNumber.setMaxLines(1);
mNumber.setSingleLine(true);
SeekBarCompat.setTextDirection(mNumber, TEXT_DIRECTION_LOCALE);
mNumber.setVisibility(View.INVISIBLE);
//add some padding for the elevation shadow not to be clipped
//I'm sure there are better ways of doing this...
setPadding(padding, padding, padding, padding);
resetSizes(maxValue);
mSeparation = (int) (SEPARATION_DP * displayMetrics.density);
int thumbSize = (int) (ThumbDrawable.DEFAULT_SIZE_DP * displayMetrics.density);
ColorStateList color = a.getColorStateList(R.styleable.DiscreteSeekBar_dsb_indicatorColor);
mMarkerDrawable = new MarkerDrawable(color, thumbSize);
mMarkerDrawable.setCallback(this);
mMarkerDrawable.setMarkerListener(this);
mMarkerDrawable.setExternalOffset(padding);
//Elevation for anroid 5+
float elevation = a.getDimension(R.styleable.DiscreteSeekBar_dsb_indicatorElevation, ELEVATION_DP * displayMetrics.density);
ViewCompat.setElevation(this, elevation);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
SeekBarCompat.setOutlineProvider(this, mMarkerDrawable);
}
a.recycle();
}
public PasswordView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//gets attributes defined by developers
final TypedArray styledData = context.obtainStyledAttributes(attrs,R.styleable.PasswordView, defStyle, 0);
//loads Drawable from resources (gets default if none is set)
mSecuredPasswordDrawable = styledData.getDrawable(R.styleable.PasswordView_securePasswordIcon);
mUnsecuredPasswordDrawable = styledData.getDrawable(R.styleable.PasswordView_unsecurePasswordIcon);
final Drawable mBackgroundDrawable = styledData.getDrawable(R.styleable.PasswordView_buttonBackground);
//if developer did not specify buttons, it takes the default ones
if(null == mSecuredPasswordDrawable) mSecuredPasswordDrawable = getResources().getDrawable(android.R.drawable.ic_secure);
if(null == mUnsecuredPasswordDrawable) mUnsecuredPasswordDrawable = getResources().getDrawable(android.R.drawable.ic_secure);
//loads validator data
passwordValidator = styledData.getInt(R.styleable.PasswordView_passwordValidator, NO_VALIDATOR);
if(passwordValidator == DEFAULT_VALIDATOR){
passwordMessages = DEFAULT_PASSWORD_MESSAGES;
passwordMessagesColors = DEFAULT_PASSWORD_COLORS;
onPasswordChangedListener = new DefaultValidator();
}
if(passwordValidator != NO_VALIDATOR){
mPasswordStrenghtLabel = new TextView(context);
mPasswordStrenghtLabel.setVisibility(INVISIBLE);
final int sidePadding = (int) styledData.getDimension(R.styleable.PasswordView_passwordMessageSidePadding, dipsToPix(LABEL_PADDING));
mPasswordStrenghtLabel.setPadding(sidePadding, 0, sidePadding, 0);
mPasswordStrenghtLabel.setTextAppearance(context, android.R.style.TextAppearance_Small);
//adds label to the View
addView(mPasswordStrenghtLabel, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
//converts side to an int dimension
final int intImageSide = dipsToPix(IMAGE_SIDE);
//creates "show password" button
mBtnShowPassword = new ImageButton(context);
mBtnShowPassword.setImageDrawable(mSecuredPasswordDrawable);
mBtnShowPassword.setEnabled(false);
mBtnShowPassword.setMaxWidth(styledData.getInt(R.styleable.PasswordView_maxButtonWidth, intImageSide));
mBtnShowPassword.setMinimumWidth(styledData.getInt(R.styleable.PasswordView_minButtonWidth, intImageSide));
mBtnShowPassword.setMaxHeight(styledData.getInt(R.styleable.PasswordView_maxButtonHeight, intImageSide));
mBtnShowPassword.setMinimumHeight(styledData.getInt(R.styleable.PasswordView_minButtonHeight, intImageSide));
//sets background if user defined it
if(null != mBackgroundDrawable)
mBtnShowPassword.setBackground(mBackgroundDrawable);
//sets listener for main behaviour
mBtnShowPassword.setOnClickListener(passwordShower);
//adds new view to the layout
addView(mBtnShowPassword, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
styledData.recycle();
}
public static void setTextAppearance(TextView textview, int style) {
textview.setTextAppearance(style);
}
private AlertDialog.Builder createDirectoryChooserDialog(String title, List<String> listItems,
DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(m_context);
// Create custom view for AlertDialog title containing
// current directory TextView and possible 'New folder' button.
// Current directory TextView allows long directory path to be wrapped to multiple lines.
LinearLayout titleLayout = new LinearLayout(m_context);
titleLayout.setOrientation(LinearLayout.VERTICAL);
m_titleView = new TextView(m_context);
m_titleView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
m_titleView.setTextAppearance(m_context, android.R.style.TextAppearance_DeviceDefault_Medium);
m_titleView.setTextColor(m_context.getResources().getColor(android.R.color.black));
m_titleView.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
m_titleView.setText(title);
Button newDirButton = new Button(m_context);
newDirButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
newDirButton.setText("New folder");
newDirButton.setOnClickListener(v -> {
final EditText input = new EditText(m_context);
// Show new folder name input dialog
new AlertDialog.Builder(m_context).
setTitle("New folder name").
setView(input).setPositiveButton(m_context.getResources().getText(R.string.global_ok), (dialog, whichButton) -> {
Editable newDir = input.getText();
String newDirName = newDir.toString();
// Create new directory
if (createSubDir(m_dir + "/" + newDirName)) {
// Navigate into the new directory
m_dir += "/" + newDirName;
updateDirectory();
} else {
Toast.makeText(
m_context, m_context.getResources().getText(R.string.failedtocreatefolder) + " " + newDirName, Toast.LENGTH_SHORT).show();
}
}).setNegativeButton(m_context.getResources().getText(R.string.global_cancel), null).show();
});
if (!m_isNewFolderEnabled) {
newDirButton.setVisibility(View.GONE);
}
titleLayout.addView(m_titleView);
titleLayout.addView(newDirButton);
dialogBuilder.setCustomTitle(titleLayout);
m_listAdapter = createListAdapter(listItems);
dialogBuilder.setSingleChoiceItems(m_listAdapter, -1, onClickListener);
dialogBuilder.setCancelable(false);
return dialogBuilder;
}