下面列出了android.widget.Button#setGravity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* set positive button
*
* @param text the name of button
*/
public void setPositiveButton(String text, final View.OnClickListener listener)
{
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setTextColor(Color.argb(255, 35, 159, 242));
button.setText(text);
button.setGravity(Gravity.CENTER);
button.setTextSize(14);
button.setPadding(dip2px(12), 0, dip2px(32), dip2px(BUTTON_BOTTOM));
button.setOnClickListener(listener);
mButtonLayout.addView(button);
}
/**
* set negative button
*
* @param text the name of button
*/
public void setNegativeButton(String text, final View.OnClickListener listener)
{
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setText(text);
button.setTextColor(Color.argb(222, 0, 0, 0));
button.setTextSize(14);
button.setGravity(Gravity.CENTER);
button.setPadding(0, 0, 0, dip2px(8));
button.setOnClickListener(listener);
if (mButtonLayout.getChildCount() > 0)
{
params.setMargins(20, 0, 10, dip2px(BUTTON_BOTTOM));
button.setLayoutParams(params);
mButtonLayout.addView(button, 1);
}
else
{
button.setLayoutParams(params);
mButtonLayout.addView(button);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final int position = getArguments().getInt("position");
String name = getArguments().getString("name");
Button btn = new Button(container.getContext(), null);
btn.setGravity(Gravity.CENTER);
btn.setText("当前状态:" + name);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity) getActivity()).setPosition(position);
}
});
return btn;
}
/**
* set negative button
*
* @param text the name of button
*/
public void setNegativeButton(String text, final View.OnClickListener listener) {
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setText(text);
button.setTextColor(Color.argb(222, 0, 0, 0));
button.setTextSize(14);
button.setGravity(Gravity.CENTER);
button.setPadding(0, 0, 0, dip2px(8));
button.setOnClickListener(listener);
if (mButtonLayout.getChildCount() > 0) {
params.setMargins(20, 0, 10, dip2px(BUTTON_BOTTOM));
button.setLayoutParams(params);
mButtonLayout.addView(button, 1);
} else {
button.setLayoutParams(params);
mButtonLayout.addView(button);
}
}
@Override
protected View getLayoutView() {
LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(ViewKnife.getColor(R.color.pd_main_bg));
layout.addView(super.getLayoutView(), new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
0,
1
));
Button button = new Button(getContext());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
getTargetFragment().onActivityResult(getTargetRequestCode(),
Activity.RESULT_OK, assembleTargetIntent());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
button.setTextSize(14);
button.setText("launch Activity");
button.setGravity(Gravity.CENTER);
button.setBackgroundResource(R.drawable.pd_shape_btn_bg);
LinearLayout.LayoutParams buttonParam;
layout.addView(button, buttonParam = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewKnife.dip2px(45)
));
buttonParam.leftMargin
= buttonParam.topMargin
= buttonParam.rightMargin
= buttonParam.bottomMargin
= ViewKnife.dip2px(16);
return layout;
}
private void initButton(Button button, String text) {
button.setText(text);
button.setTextSize(22);
button.setPadding(0, 0, 0, 0);
button.setWidth((int)(30 * density));
button.setHeight((int)(30 * density));
button.setSingleLine();
button.setGravity(Gravity.CENTER);
}
/**
* set positive button
*
* @param text the name of button
*/
public void setPositiveButton(String text, final View.OnClickListener listener) {
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setTextColor(Color.argb(255, 35, 159, 242));
button.setText(text);
button.setGravity(Gravity.CENTER);
button.setTextSize(14);
button.setPadding(dip2px(12), 0, dip2px(32), dip2px(BUTTON_BOTTOM));
button.setOnClickListener(listener);
mButtonLayout.addView(button);
}
/**
* Create And Add Layout
*/
private void createAndAddLayout() {
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setWeightSum(1f);
linearLayout.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams tvLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
tvLayoutParams.weight = 0.7f;
LinearLayout.LayoutParams showButtonLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
showButtonLayoutParams.weight = 0.3f;
tvText = new TextView(context);
tvText.setLayoutParams(tvLayoutParams);
tvText.setTextColor(ContextCompat.getColor(context, R.color.top_snackbar_text_color));
tvText.setPadding(24, 0, 0, 0);
linearLayout.addView(tvText);
showButton = new Button(context);
showButton.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
showButton.setTextColor(ContextCompat.getColor(context, R.color.top_snackbar_show_button_text_color));
showButton.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
showButton.setLayoutParams(showButtonLayoutParams);
linearLayout.addView(showButton);
linearLayout.setVisibility(View.GONE);
linearLayout.setBackgroundResource(R.drawable.top_snackbar_bg);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
drawer = (MaterialNavigationDrawer) getActivity();
Button button = new Button(this.getActivity());
button.setText("start child fragment");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer.changeFragment(new ChildFragment2(), "Child Title 2");
// normally currentSection gets unselect on setCustomFragment call
// in the next relase, i will add a new method without unselect
drawer.getCurrentSectionFragment().select();
// call on current git head. drawer.getCurrentSectionFragment().select(); is not needed
// drawer.setCustomFragment(drawer.getCurrentSectionFragment().getTargetFragment(), drawer.getCurrentSectionFragment().getFragmentTitle(), true, false);
}
});
return button;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
drawer = (MaterialNavigationDrawer) getActivity();
Button button = new Button(this.getActivity());
button.setText("start child fragment");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer.changeFragment(new ChildFragment(), "Child Title");
// normally currentSection gets unselect on setCustomFragment call
// in the next relase, i will add a new method without unselect
drawer.getCurrentSectionFragment().select();
// call on current git head. drawer.getCurrentSectionFragment().select(); is not needed
// drawer.setCustomFragment(drawer.getCurrentSectionFragment().getTargetFragment(), drawer.getCurrentSectionFragment().getFragmentTitle(), true, false);
}
});
return button;
}
/**
* {@inheritDoc}
*/
@Override
protected View createView(Context c) {
et = new EditText(c);
et.setPadding(10, 5, 10, 5);
et.setText(answer);
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
et.setGravity(Gravity.CENTER_HORIZONTAL);
et.setKeyListener(new DialerKeyListener());
LinearLayout ll = new LinearLayout(c);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(et, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
boolean barcodeEnable = true; //sp.getBoolean(Constants.PREFERENCE_BARCODE_ENABLED, false);
if (barcodeEnable) {
barcodeButton = new Button(c);
barcodeButton.setText(c.getResources().getString(
R.string.procedurerunner_scan_id));
barcodeButton.setOnClickListener(this);
barcodeButton.setGravity(Gravity.CENTER_HORIZONTAL);
ll.addView(barcodeButton, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
return encapsulateQuestion(c, ll);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Button button = new Button(this.getActivity());
button.setText("Click Me");
button.setGravity(Gravity.CENTER);
return button;
}
private void addAButton(final String text) {
int dimension = (int) PixelDipConverter.convertDpToPixel(50, getContext());
//int padding = (int) PixelDipConverter.convertDpToPixel(10, getContext());
final Button name = new Button(getContext());
name.setLayoutParams(new LinearLayout.LayoutParams(dimension, dimension));
name.setGravity(Gravity.CENTER);
name.setText(text);
name.setTextSize(15);
name.setAllCaps(true);
//name.setPadding(padding, padding, padding, padding);
name.setClickable(true);
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
iAccessoryView.onButtonAccessoryViewClicked(text);
}
});
name.setBackgroundResource(outValue.resourceId);
addView(name);
}
private void initComponents() {
LinearLayout llRoot = (LinearLayout)this.findViewById(R.id.llRoot);
LinearLayout llHeaderBase = (LinearLayout)findViewById(R.id.llHeaderBase);
LinearLayout llHeaderSearch = (LinearLayout)findViewById(R.id.llHeaderSearch);
EditText etKeyWord = (EditText) findViewById(R.id.etKeyWord);
Button btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearchStatus = (Button) findViewById(R.id.btnSearchStatus);
btnSearchUser = (Button) findViewById(R.id.btnSearchUser);
lvSearchResult = (ListView) findViewById(R.id.lvSearchResult);
lvSearchResult.setFastScrollEnabled(sheJiaoMao.isSliderEnabled());
lvSearchResult.setOnScrollListener(new StatusScrollListener());
ThemeUtil.setRootBackground(llRoot);
ThemeUtil.setSecondaryHeader(llHeaderBase);
llHeaderSearch.setBackgroundDrawable(theme.getDrawable("bg_header_corner_search"));
int padding6 = theme.dip2px(6);
int padding8 = theme.dip2px(8);
llHeaderSearch.setPadding(padding6, padding8, padding6, padding8);
ThemeUtil.setListViewStyle(lvSearchResult);
etKeyWord.setBackgroundDrawable(theme.getDrawable("bg_input_frame_left_half"));
btnSearch.setBackgroundDrawable(theme.getDrawable("selector_btn_search"));
btnSearchStatus.setBackgroundDrawable(theme.getDrawable("selector_tab_toggle_left"));
btnSearchStatus.setPadding(0, 0, 0, 0);
ColorStateList selectorBtnTab = theme.getColorStateList("selector_btn_tab");
btnSearchStatus.setTextColor(selectorBtnTab);
btnSearchStatus.setGravity(Gravity.CENTER);
btnSearchUser.setBackgroundDrawable(theme.getDrawable("selector_tab_toggle_right"));
btnSearchUser.setPadding(0, 0, 0, 0);
btnSearchUser.setTextColor(selectorBtnTab);
btnSearchUser.setGravity(Gravity.CENTER);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void init(AttributeSet attrs, int defStyle) {
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.QuantityView, defStyle, 0);
addButtonText = getResources().getString(R.string.qv_add);
if (a.hasValue(R.styleable.QuantityView_qv_addButtonText)) {
addButtonText = a.getString(R.styleable.QuantityView_qv_addButtonText);
}
addButtonBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_btn_selector);
if (a.hasValue(R.styleable.QuantityView_qv_addButtonBackground)) {
addButtonBackground = a.getDrawable(R.styleable.QuantityView_qv_addButtonBackground);
}
addButtonTextColor = a.getColor(R.styleable.QuantityView_qv_addButtonTextColor, Color.BLACK);
removeButtonText = getResources().getString(R.string.qv_remove);
if (a.hasValue(R.styleable.QuantityView_qv_removeButtonText)) {
removeButtonText = a.getString(R.styleable.QuantityView_qv_removeButtonText);
}
removeButtonBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_btn_selector);
if (a.hasValue(R.styleable.QuantityView_qv_removeButtonBackground)) {
removeButtonBackground = a.getDrawable(R.styleable.QuantityView_qv_removeButtonBackground);
}
removeButtonTextColor = a.getColor(R.styleable.QuantityView_qv_removeButtonTextColor, Color.BLACK);
quantity = a.getInt(R.styleable.QuantityView_qv_quantity, 0);
maxQuantity = a.getInt(R.styleable.QuantityView_qv_maxQuantity, Integer.MAX_VALUE);
minQuantity = a.getInt(R.styleable.QuantityView_qv_minQuantity, 0);
quantityPadding = (int) a.getDimension(R.styleable.QuantityView_qv_quantityPadding, pxFromDp(24));
quantityTextColor = a.getColor(R.styleable.QuantityView_qv_quantityTextColor, Color.BLACK);
quantityBackground = ContextCompat.getDrawable(getContext(), R.drawable.qv_bg_selector);
if (a.hasValue(R.styleable.QuantityView_qv_quantityBackground)) {
quantityBackground = a.getDrawable(R.styleable.QuantityView_qv_quantityBackground);
}
quantityDialog = a.getBoolean(R.styleable.QuantityView_qv_quantityDialog, true);
a.recycle();
int dp10 = pxFromDp(10);
mButtonAdd = new Button(getContext());
mButtonAdd.setGravity(Gravity.CENTER);
mButtonAdd.setPadding(dp10, dp10, dp10, dp10);
mButtonAdd.setMinimumHeight(0);
mButtonAdd.setMinimumWidth(0);
mButtonAdd.setMinHeight(0);
mButtonAdd.setMinWidth(0);
setAddButtonBackground(addButtonBackground);
setAddButtonText(addButtonText);
setAddButtonTextColor(addButtonTextColor);
mButtonRemove = new Button(getContext());
mButtonRemove.setGravity(Gravity.CENTER);
mButtonRemove.setPadding(dp10, dp10, dp10, dp10);
mButtonRemove.setMinimumHeight(0);
mButtonRemove.setMinimumWidth(0);
mButtonRemove.setMinHeight(0);
mButtonRemove.setMinWidth(0);
setRemoveButtonBackground(removeButtonBackground);
setRemoveButtonText(removeButtonText);
setRemoveButtonTextColor(removeButtonTextColor);
mTextViewQuantity = new TextView(getContext());
mTextViewQuantity.setGravity(Gravity.CENTER);
setQuantityTextColor(quantityTextColor);
setQuantity(quantity);
setQuantityBackground(quantityBackground);
setQuantityPadding(quantityPadding);
setOrientation(HORIZONTAL);
addView(mButtonRemove, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(mTextViewQuantity, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
addView(mButtonAdd, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mButtonAdd.setOnClickListener(this);
mButtonRemove.setOnClickListener(this);
mTextViewQuantity.setOnClickListener(this);
}