本文整理汇总了Java中android.widget.Button.setGravity方法的典型用法代码示例。如果您正苦于以下问题:Java Button.setGravity方法的具体用法?Java Button.setGravity怎么用?Java Button.setGravity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.widget.Button
的用法示例。
在下文中一共展示了Button.setGravity方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setPositiveButton
import android.widget.Button; //导入方法依赖的package包/类
/**
* 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);
}
示例2: setNegativeButton
import android.widget.Button; //导入方法依赖的package包/类
/**
* 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);
}
}
示例3: generateTopBarTextButton
import android.widget.Button; //导入方法依赖的package包/类
/**
* 生成一个文本按钮,并设置文字
*
* @param text 按钮的文字
* @return
*/
private Button generateTopBarTextButton(String text) {
Button button = new Button(getContext());
button.setBackgroundResource(0);
button.setMinWidth(0);
button.setMinHeight(0);
button.setMinimumWidth(0);
button.setMinimumHeight(0);
int paddingHorizontal = getTopBarTextBtnPaddingHorizontal();
button.setPadding(paddingHorizontal, 0, paddingHorizontal, 0);
button.setTextColor(QMUIResHelper.getAttrColorStateList(getContext(), R.attr.qmui_topbar_text_btn_color_state_list));
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_topbar_text_btn_text_size));
button.setGravity(Gravity.CENTER);
button.setText(text);
return button;
}
示例4: getButton
import android.widget.Button; //导入方法依赖的package包/类
private Button getButton(String text, int position) {
// 动态生成选择按钮
final Button button = new Button(mContext);
button.setText(text);
button.setTag(position);
button.setTextColor(mBuilder.getItemTextColor());
button.setTextSize(mBuilder.getItemTextSize());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams
.MATCH_PARENT, mBuilder.getItemHeight());
button.setLayoutParams(lp);
button.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
button.setPadding(UiUtils.dp2px(mContext,10),0,UiUtils.dp2px(mContext,10),0);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (mBuilder.getOnItemListener() != null) {
selectPosition = Integer.parseInt(button.getTag().toString());
mBuilder.getOnItemListener().onItemClick(button, selectPosition);
}
}
});
return button;
}
示例5: addButton
import android.widget.Button; //导入方法依赖的package包/类
private void addButton(final DemoInfo leaf){
Button btn = new Button(getActivity());
btn.setText(leaf.name);
btn.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
btn.setBackgroundResource(R.drawable.sel_menu3);
btn.setTextSize(15);
btn.setPadding(20, 0, 20, 0);
btn.setTextColor(Color.WHITE);
btn.setAllCaps(false);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, Utils.dip2px(getActivity(), 40));
lp.gravity = Gravity.CENTER;
lp.topMargin = Utils.dip2px(getActivity(), 5);
lp.bottomMargin = Utils.dip2px(getActivity(), 5);
lp.leftMargin = Utils.dip2px(getActivity(), 5);
lp.rightMargin = Utils.dip2px(getActivity(), 5);
ll_root.addView(btn, lp);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(leaf.onClickListener == null){
Toast.makeText(getActivity(), "sorry,此功能尚未实现", Toast.LENGTH_SHORT).show();
}else{
leaf.onClickListener.onClick(v);
}
}
});
}
示例6: generateSpanActionButton
import android.widget.Button; //导入方法依赖的package包/类
/**
* 生成适用于对话框的按钮
*/
public static Button generateSpanActionButton(Context context, String text, int iconRes, boolean hasLeftMargin) {
Button button = new Button(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_height));
if(hasLeftMargin){
lp.leftMargin = QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_margin_left);
}
button.setLayoutParams(lp);
button.setMinHeight(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_height));
button.setMinWidth(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_min_width));
button.setMinimumWidth(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_min_width));
button.setMinimumHeight(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_height));
button.setText(text);
if (iconRes != 0) {
Drawable drawable = context.getResources().getDrawable(iconRes);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
button.setCompoundDrawables(drawable, null, null, null);
button.setCompoundDrawablePadding(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_drawable_padding));
}
}
button.setGravity(Gravity.CENTER);
button.setClickable(true);
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_text_size));
button.setTextColor(QMUIResHelper.getAttrColorStateList(context, R.attr.qmui_dialog_action_text_color));
button.setBackground(QMUIResHelper.getAttrDrawable(context, R.attr.qmui_dialog_action_btn_bg));
final int paddingHor = QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_padding_horizontal);
button.setPadding(paddingHor, 0, paddingHor, 0);
return button;
}
示例7: addAButton
import android.widget.Button; //导入方法依赖的package包/类
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);
}
示例8: generateSpanActionButton
import android.widget.Button; //导入方法依赖的package包/类
/**
* 生成适用于对话框的按钮
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static Button generateSpanActionButton(Context context, String text, int iconRes, boolean hasLeftMargin) {
Button button = new Button(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_height));
if(hasLeftMargin){
lp.leftMargin = QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_margin_left);
}
button.setLayoutParams(lp);
button.setMinHeight(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_height));
button.setMinWidth(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_min_width));
button.setMinimumWidth(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_min_width));
button.setMinimumHeight(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_height));
button.setText(text);
if (iconRes != 0) {
Drawable drawable = ContextCompat.getDrawable(context, iconRes);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
button.setCompoundDrawables(drawable, null, null, null);
button.setCompoundDrawablePadding(QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_drawable_padding));
}
}
button.setGravity(Gravity.CENTER);
button.setClickable(true);
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_text_size));
button.setTextColor(QMUIResHelper.getAttrColorStateList(context, R.attr.qmui_dialog_action_text_color));
button.setBackground(QMUIResHelper.getAttrDrawable(context, R.attr.qmui_dialog_action_btn_bg));
final int paddingHor = QMUIResHelper.getAttrDimen(context, R.attr.qmui_dialog_action_button_padding_horizontal);
button.setPadding(paddingHor, 0, paddingHor, 0);
return button;
}
示例9: generateTopBarTextButton
import android.widget.Button; //导入方法依赖的package包/类
/**
* 生成一个文本按钮,并设置文字
*
* @param text 按钮的文字
* @return 返回生成的按钮
*/
private Button generateTopBarTextButton(String text) {
Button button = new Button(getContext());
button.setBackgroundResource(0);
button.setMinWidth(0);
button.setMinHeight(0);
button.setMinimumWidth(0);
button.setMinimumHeight(0);
button.setPadding(mTopBarTextBtnPaddingHor, 0, mTopBarTextBtnPaddingHor, 0);
button.setTextColor(mTopBarTextBtnTextColor);
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTopBarTextBtnTextSize);
button.setGravity(Gravity.CENTER);
button.setText(text);
return button;
}
示例10: addButton
import android.widget.Button; //导入方法依赖的package包/类
private void addButton(final Leaf leaf){
Button btn = new Button(getActivity());
btn.setText(leaf.name);
btn.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
btn.setBackgroundResource(R.drawable.sel_menu2);
btn.setTextSize(15);
btn.setPadding(20, 0, 20, 0);
btn.setTextColor(Color.WHITE);
btn.setAllCaps(false);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, Utils.dip2px(getActivity(), 40));
lp.gravity = Gravity.CENTER;
lp.topMargin = Utils.dip2px(getActivity(), 5);
lp.bottomMargin = Utils.dip2px(getActivity(), 5);
lp.leftMargin = Utils.dip2px(getActivity(), 5);
lp.rightMargin = Utils.dip2px(getActivity(), 5);
ll_root.addView(btn, lp);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(leaf.attacherClass != null){
Master.startPage(getActivity(), leaf.attacherClass, null);
}else if(leaf.activityClass != null){
Intent intent = new Intent(getActivity(), leaf.activityClass);
getActivity().startActivity(intent);
}else{
Toast.makeText(getActivity(), "sorry,此功能尚未实现", Toast.LENGTH_SHORT).show();
}
}
});
if(leaf.attacherClass == null && leaf.activityClass == null){
}else{
btn.setBackgroundResource(R.drawable.sel_menu3);
}
}
示例11: init
import android.widget.Button; //导入方法依赖的package包/类
private void init(String text, int iconRes) {
LinearLayout.LayoutParams parentLp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_action_block_btn_height));
setLayoutParams(parentLp);
setBackground(QMUIResHelper.getAttrDrawable(getContext(), R.attr.qmui_dialog_action_block_btn_bg));
setPadding(
QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_padding_horizontal),
0,
QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_padding_horizontal),
0
);
mButton = new Button(getContext());
mButton.setBackgroundResource(0);
mButton.setPadding(0, 0, 0, 0);
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.RIGHT;
mButton.setLayoutParams(lp);
mButton.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
mButton.setText(text);
if (iconRes != 0) {
Drawable drawable = ContextCompat.getDrawable(getContext(), iconRes);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
mButton.setCompoundDrawables(drawable, null, null, null);
mButton.setCompoundDrawablePadding(QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_action_drawable_padding));
}
}
mButton.setMinHeight(0);
mButton.setMinWidth(0);
mButton.setMinimumWidth(0);
mButton.setMinimumHeight(0);
mButton.setClickable(false);
mButton.setDuplicateParentStateEnabled(true);
mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_action_button_text_size));
mButton.setTextColor(QMUIResHelper.getAttrColorStateList(getContext(), R.attr.qmui_dialog_action_text_color));
addView(mButton);
}
示例12: init
import android.widget.Button; //导入方法依赖的package包/类
private void init(String text, int iconRes) {
LinearLayout.LayoutParams parentLp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_action_block_btn_height));
setLayoutParams(parentLp);
QMUIViewHelper.setBackgroundKeepingPadding(this, QMUIResHelper.getAttrDrawable(getContext(), R.attr.qmui_dialog_action_block_btn_bg));
setPadding(
QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_padding_horizontal),
0,
QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_padding_horizontal),
0
);
mButton = new Button(getContext());
mButton.setBackgroundResource(0);
mButton.setPadding(0, 0, 0, 0);
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.RIGHT;
mButton.setLayoutParams(lp);
mButton.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
mButton.setText(text);
if (iconRes != 0) {
Drawable drawable = ContextCompat.getDrawable(getContext(), iconRes);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
mButton.setCompoundDrawables(drawable, null, null, null);
mButton.setCompoundDrawablePadding(QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_action_drawable_padding));
}
}
mButton.setMinHeight(0);
mButton.setMinWidth(0);
mButton.setMinimumWidth(0);
mButton.setMinimumHeight(0);
mButton.setClickable(false);
mButton.setDuplicateParentStateEnabled(true);
mButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, QMUIResHelper.getAttrDimen(getContext(), R.attr.qmui_dialog_action_button_text_size));
mButton.setTextColor(QMUIResHelper.getAttrColorStateList(getContext(), R.attr.qmui_dialog_action_text_color));
addView(mButton);
}