下面列出了android.widget.Button#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setMiningButtonState(Boolean state) {
Button btn = findViewById(R.id.start);
Drawable buttonDrawable = btn.getBackground();
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
if (minerPaused) {
btn.setText("Resume");
DrawableCompat.setTint(buttonDrawable, Color.rgb(238, 201, 0));
btn.setBackground(buttonDrawable);
} else {
if (state) {
btn.setText("Stop");
DrawableCompat.setTint(buttonDrawable, Color.rgb(153, 0, 0));
btn.setBackground(buttonDrawable);
} else {
btn.setText("Start");
DrawableCompat.setTint(buttonDrawable, Color.rgb(0, 153, 0));
btn.setBackground(buttonDrawable);
}
}
}
private void gridButtonClicked(int col, int row) {
Toast.makeText(this, "Button clicked: " + col + "," + row,
Toast.LENGTH_SHORT).show();
Button button = buttons[row][col];
// Lock Button Sizes:
lockButtonSizes();
// Does not scale image.
// button.setBackgroundResource(R.drawable.action_lock_pink);
// Scale image to button: Only works in JellyBean!
// Image from Crystal Clear icon set, under LGPL
// http://commons.wikimedia.org/wiki/Crystal_Clear
int newWidth = button.getWidth();
int newHeight = button.getHeight();
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.action_lock_pink);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, true);
Resources resource = getResources();
button.setBackground(new BitmapDrawable(resource, scaledBitmap));
// Change text on button:
button.setText("" + col);
}
private Button addCancelButton(){
mBtnCancel = new Button(mContext);
LayoutParams lp = new LayoutParams(0,
LayoutParams.WRAP_CONTENT);
lp.weight = 1;
lp.setMargins(0, 0, mMargin16, 0);
mBtnCancel.setLayoutParams(lp);
mBtnCancel.setText(mBtnCancelStr);
mBtnCancel.setTextColor(mButtonTextColor);
mBtnCancel.setOnClickListener(onCancelPatternListener);
if (mButtonBgResource != null){
mBtnCancel.setBackground(mButtonBgResource);
}
mBtnCancel.setVisibility(INVISIBLE);
return mBtnCancel;
}
/**
* Change the button's drawable
*/
public void setButtonState(final boolean bRecording) {
final Runnable runnableUi = new Runnable(){
@Override
public void run() {
int iDrawable = bRecording ? R.drawable.button_record_stop : R.drawable.button_record_start;
Button btnRecord = (Button)mView.findViewById(R.id.buttonRecord);
btnRecord.setBackground(getResources().getDrawable(iDrawable));
}
};
new Thread(){
public void run(){
mHandler.post(runnableUi);
}
}.start();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void showButtonIcon(Button backButton, String iconPath) {
if(""!=iconPath && null!=iconPath){
String IconImg=iconPath;
IconImg=IconImg.substring(BUtility.F_Widget_RES_SCHEMA
.length());
IconImg = BUtility.F_Widget_RES_path + IconImg;
Bitmap leftIconImgBitmap =((EBrowserActivity)mContext).getImage(IconImg);
if(null!=IconImg){
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),
leftIconImgBitmap);
if(null!=bitmapDrawable){
backButton.setBackground(bitmapDrawable);
}
}
}else{
backButton.setVisibility(GONE);
}
}
private void setButtonColor(Button button, int index) {
ButtonColor bc = ButtonColorList.getButtonColor(index);
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {bc.getE(), bc.getC() });
gd.setCornerRadius(0f);
gd.setStroke(1, bc.getE());
gd.setCornerRadius(3f);
if (android.os.Build.VERSION.SDK_INT >= JELLY_BEAN_MR1)
button.setBackground(gd);
else
button.setBackgroundDrawable(gd);
button.setTextColor(bc.getTextColor());
button.setShadowLayer(1f, 1f, 1f, bc.getE());
LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) button.getLayoutParams();
params.setMargins(5, 5, 5, 5);
button.setLayoutParams(params);
}
public static void setButtonBgColorFromHex(Button button, String hexColor) {
try {
Drawable drawable = button.getBackground();
Drawable compatDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(compatDrawable, Color.parseColor(hexColor));
button.setBackground(compatDrawable);
} catch (IllegalArgumentException e) {
// If the color didnt parse correctly, fail 'open', with default background color
Logging.loge("Error parsing background color: " + e.toString());
}
}
public void newsfeed(View view) {
View uploaddetails = (View) findViewById(R.id.uploaddetails);
uploaddetails.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
Button uploadbutton = (Button) findViewById(R.id.uploadbutton);
Button newsfeedbutton = (Button) findViewById(R.id.newsfeedbutton);
uploadbutton.setBackground(ContextCompat.getDrawable(this, R.drawable.b3));
uploadbutton.setTextColor(getResources().getColor(R.color.white));
newsfeedbutton.setTextColor(getResources().getColor(R.color.colorPrimary));
newsfeedbutton.setBackground(ContextCompat.getDrawable(this, R.drawable.b2));
}
private Button addConfirmButton(){
mBtnConfirm = new Button(mContext);
LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
lp.weight = 1;
lp.setMargins(mMargin16, 0, 0, 0);
mBtnConfirm.setLayoutParams(lp);
mBtnConfirm.setText(mBtnRepeatStr);
mBtnConfirm.setTextColor(mButtonTextColor);
mBtnConfirm.setOnClickListener(onConfirmPatternListener);
if (mButtonBgResource != null){
mBtnConfirm.setBackground(mButtonBgResource);
}
mBtnConfirm.setVisibility(INVISIBLE);
return mBtnConfirm;
}
public static LinearLayout.LayoutParams styleButton(Context context, Button button) {
button.setTextColor(Compat.getColor(context, R.color.main_text_color));
Compat.setButtonTextAppearance(context, button, android.R.attr.textAppearanceMedium);
button.setBackground(Compat.getDrawable(context, R.drawable.button_background_drawable));
int pad = (int) context.getResources().getDimension(R.dimen.button_indent);
button.setPadding(pad, pad, pad, pad);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(15, 15, 15, 15);
return lp;
}
@TargetApi(16)
public void setButtonBackgroundAPI16Above(Button b, int i) {
b.setBackground(ResourcesCompat.getDrawable(getResources(), buttonicon[i], null));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.parsed_msg, parent,
false);
}
Button btn = (Button) convertView
.findViewById(R.id.list_item_btn);
ImageView img = (ImageView) convertView
.findViewById(R.id.list_item_img);
View separator = convertView
.findViewById(R.id.separator);
Message msg = (Message)getItem(position);
// handling for separator/break
if (msg.type == MSG_BREAK) {
btn.setVisibility(View.GONE);
img.setVisibility(View.GONE);
separator.setVisibility(View.VISIBLE);
return convertView;
}
btn.setText(msg.text);
btn.setVisibility(View.VISIBLE);
// img visibility handled below
separator.setVisibility(View.GONE);
// handling based on presence of parsed message contents
if (msg.parsed.isEmpty()) {
btn.setEnabled(false);
btn.setBackground(null);
img.setVisibility(View.GONE);
} else {
btn.setEnabled(true);
btn.setBackground(mContext.getResources().
getDrawable(R.drawable.parsed_msg_bg_states));
img.setVisibility(View.VISIBLE);
btn.setOnClickListener(new MessageClickListener(position));
}
// default style
int color = android.R.color.black;
// specific message type styles
switch (msg.type) {
case MSG_SEND:
color = R.color.msg_send;
break;
case MSG_RCV:
color = R.color.msg_rcv;
break;
case MSG_OKAY:
color = R.color.msg_okay;
break;
case MSG_ERROR:
color = R.color.msg_err;
break;
}
btn.setTextColor(mContext.getResources().getColor(color));
return convertView;
}
/**
* @param context the context to use.
* @param attrs attributes.
* @param parentView parent
* @param label label
* @param value value
* @param type the text type.
* @param constraintDescription constraints
* @param readonly if <code>false</code>, the item is disabled for editing.
*/
public GDynamicEditTextView(final Context context, AttributeSet attrs, LinearLayout parentView, String label, String value, final int type,
String constraintDescription, final boolean readonly) {
super(context, attrs);
editViewList = new ArrayList<>();
mainLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
mainLayout.setLayoutParams(layoutParams);
mainLayout.setOrientation(LinearLayout.VERTICAL);
parentView.addView(mainLayout);
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(2, 2, 2, 2);
textView.setText(label.replace(UNDERSCORE, " ").replace(COLON, " ") + " " + constraintDescription);
textView.setTextColor(Compat.getColor(context, R.color.formcolor));
mainLayout.addView(textView);
String[] valuesSplit = value.trim().split(";");
if (valuesSplit.length == 0) {
valuesSplit = new String[]{" "};
}
for (String singleValue : valuesSplit) {
addSingleEditText(context, mainLayout, singleValue.trim(), readonly, type);
}
float minTouch = context.getResources().getDimension(R.dimen.min_touch_size);
addTextButton = new Button(context);
addTextButton.setLayoutParams(new LinearLayout.LayoutParams((int) minTouch, (int) minTouch));
addTextButton.setPadding(5, 5, 5, 5);
// addTextButton.setText("+");
addTextButton.setBackground(Compat.getDrawable(context, R.drawable.ic_add_primary_24dp));
mainLayout.addView(addTextButton);
addTextButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mainLayout.removeView(addTextButton);
addSingleEditText(context, mainLayout, "", readonly, type);
// add the button back
mainLayout.addView(addTextButton);
}
});
}