android.widget.Button.setGravity()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(13.2k)|赞(0)|评价(0)|浏览(174)

本文整理了Java中android.widget.Button.setGravity()方法的一些代码示例,展示了Button.setGravity()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Button.setGravity()方法的具体详情如下:
包路径:android.widget.Button
类名称:Button
方法名:setGravity

Button.setGravity介绍

暂无

代码示例

代码示例来源:origin: jaydenxiao2016/AndroidFire

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;
}

代码示例来源:origin: stackoverflow.com

Button yourNewButton = new Button (getBaseContext());
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
          LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 0, 0, 0);
yourNewButton.setLayoutParams(lparams);
yourNewButton.setPadding(0, 0, 0, 0);
yourNewButton.setGravity(Gravity.CENTER);
yourNewButton.setBackgroundResource(R.drawable.yourBackground);
LinearLayout layButtons = (LinearLayout) findViewById(R.id.buttonContainer);
layButtons.addView(yourNewButton);

代码示例来源:origin: cyclestreets/android

private void drawSegmentInfo() {
 final Segment seg = Route.journey().activeSegment();
 if (seg == null) {
  // In this case, populating the routing info is done by the TapToRouteOverlay
  return;
 }
 routeNowIcon.setVisibility(View.INVISIBLE);
 routingInfoRect.setBackgroundColor(highlightColour);
 routingInfoRect.setGravity(Gravity.LEFT);
 routingInfoRect.setText(seg.toString());
 routingInfoRect.setEnabled(false);
}

代码示例来源:origin: stackoverflow.com

home.setLayoutParams(new LayoutParams(tooBarHeight,LayoutParams.MATCH_PARENT));
home.setGravity(Gravity.RIGHT & Gravity.CENTER_VERTICAL);
topBar.addView(home);
order.setGravity(Gravity.RIGHT & Gravity.CENTER_VERTICAL);

代码示例来源:origin: q805699513/OptionFrame

/**
 * 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);
}

代码示例来源:origin: zulip/zulip-android

/**
 * 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);
}

代码示例来源:origin: szpnygo/NoWordsChat

/**
 * set positive button
 *
 * @param text     the name of button
 * @param listener
 */
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(12), dip2px(BUTTON_BOTTOM));
  button.setOnClickListener(listener);
  mButtonLayout.addView(button);
}

代码示例来源:origin: szpnygo/NoWordsChat

public MaterialDialog setNegativeButton(String text, final View.OnClickListener listener) {
  mNegativeButton = new Button(mContext);
  mLayoutParams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT,
      LinearLayout.LayoutParams.WRAP_CONTENT
  );
  mNegativeButton.setLayoutParams(mLayoutParams);
  mNegativeButton.setBackgroundResource(R.drawable.button);
  mNegativeButton.setText(text);
  mNegativeButton.setTextColor(Color.argb(222, 0, 0, 0));
  mNegativeButton.setTextSize(14);
  mNegativeButton.setGravity(Gravity.CENTER);
  mNegativeButton.setOnClickListener(listener);
  if (isLollipop()) {
    mNegativeButton.setBackgroundResource(android.R.color.transparent);
  }
  return this;
}

代码示例来源:origin: szpnygo/NoWordsChat

public MaterialDialog setNegativeButton(int resId, final View.OnClickListener listener) {
  mNegativeButton = new Button(mContext);
  mLayoutParams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT,
      LinearLayout.LayoutParams.WRAP_CONTENT
  );
  mNegativeButton.setLayoutParams(mLayoutParams);
  mNegativeButton.setBackgroundResource(R.drawable.button);
  mNegativeButton.setText(resId);
  mNegativeButton.setTextColor(Color.argb(222, 0, 0, 0));
  mNegativeButton.setTextSize(14);
  mNegativeButton.setGravity(Gravity.CENTER);
  mNegativeButton.setOnClickListener(listener);
  if (isLollipop()) {
    mNegativeButton.setBackgroundResource(android.R.color.transparent);
  }
  return this;
}

代码示例来源:origin: q805699513/OptionFrame

/**
 * 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);
  }
}

代码示例来源:origin: szpnygo/NoWordsChat

/**
 * set negative button
 *
 * @param text     the name of button
 * @param listener
 */
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);
  }
}

代码示例来源:origin: szpnygo/NoWordsChat

public MaterialDialog setPositiveButton(String text, final View.OnClickListener listener) {
  mPositiveButton = new Button(mContext);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT,
      LinearLayout.LayoutParams.WRAP_CONTENT
  );
  mPositiveButton.setLayoutParams(params);
  mPositiveButton.setBackgroundResource(R.drawable.button);
  mPositiveButton.setTextColor(Color.argb(255, 35, 159, 242));
  mPositiveButton.setText(text);
  mPositiveButton.setGravity(Gravity.CENTER);
  mPositiveButton.setTextSize(14);
  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT,
      LinearLayout.LayoutParams.WRAP_CONTENT
  );
  layoutParams.setMargins(dip2px(2), 0, dip2px(12), dip2px(BUTTON_BOTTOM));
  mPositiveButton.setLayoutParams(layoutParams);
  mPositiveButton.setOnClickListener(listener);
  if (isLollipop()) {
    mPositiveButton.setBackgroundResource(android.R.color.transparent);
  }
  return this;
}

代码示例来源:origin: szpnygo/NoWordsChat

public MaterialDialog setPositiveButton(int resId, final View.OnClickListener listener) {
  mPositiveButton = new Button(mContext);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT,
      LinearLayout.LayoutParams.WRAP_CONTENT
  );
  mPositiveButton.setLayoutParams(params);
  mPositiveButton.setBackgroundResource(R.drawable.button);
  mPositiveButton.setTextColor(Color.argb(255, 35, 159, 242));
  mPositiveButton.setText(resId);
  mPositiveButton.setGravity(Gravity.CENTER);
  mPositiveButton.setTextSize(14);
  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT,
      LinearLayout.LayoutParams.WRAP_CONTENT
  );
  layoutParams.setMargins(dip2px(2), 0, dip2px(12), dip2px(BUTTON_BOTTOM));
  mPositiveButton.setLayoutParams(layoutParams);
  mPositiveButton.setOnClickListener(listener);
  if (isLollipop()) {
    mPositiveButton.setBackgroundResource(android.R.color.transparent);
  }
  return this;
}

代码示例来源:origin: stackoverflow.com

if (linearLayout.getChildAt(i) instanceof Button) {
  final Button child = (Button) linearLayout.getChildAt(i);
  child.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
  final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) child.getLayoutParams();
  params.width = LinearLayout.LayoutParams.MATCH_PARENT;

代码示例来源:origin: yaozs/YzsLib

mButtonAdd.setGravity(Gravity.CENTER);
mButtonAdd.setPadding(dp10, dp10, dp10, dp10);
mButtonAdd.setMinimumHeight(0);
mButtonRemove.setGravity(Gravity.CENTER);
mButtonRemove.setPadding(dp10, dp10, dp10, dp10);
mButtonRemove.setMinimumHeight(0);

代码示例来源:origin: weavey/NormalSelectDialog

private Button getButton(String text, int position) {
  // 动态生成选择按钮
  final Button button = new Button(mBuilder.getContext());
  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(mBuilder.getContext(), 10), 0, UiUtils.dp2px(mBuilder
      .getContext(), 10), 0);
  button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
      if (mBuilder.getOnItemListener() != null) {
        clickPosition = Integer.parseInt(button.getTag().toString());
        mBuilder.getOnItemListener().onItemClick(MDSelectionDialog.this,button, clickPosition);
      }
    }
  });
  return button;
}

代码示例来源:origin: wasdennnoch/AndroidN-ify

@Override
  public void handleLayoutInflated(XC_LayoutInflated.LayoutInflatedParam liparam) throws Throwable {
    Button button = (Button) liparam.view;
    Context context = button.getContext();
    ResourceUtils res = ResourceUtils.getInstance(context);
    int sidePadding = res.getDimensionPixelSize(R.dimen.notification_actions_margin_start);
    int topBottomPadding = res.getDimensionPixelSize(R.dimen.notification_action_button_padding);
    ViewGroup.MarginLayoutParams buttonLp = new FrameLayout.LayoutParams(WRAP_CONTENT, res.getDimensionPixelSize(R.dimen.notification_action_button_height), Gravity.CENTER);
    buttonLp.setMarginStart(res.getDimensionPixelSize(R.dimen.notification_action_button_margin_start));
    button.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
    button.setPadding(sidePadding, topBottomPadding, sidePadding, topBottomPadding);
    button.setTextSize(TypedValue.COMPLEX_UNIT_PX, res.getDimensionPixelSize(R.dimen.notification_action_button_text_size));
    button.setLayoutParams(buttonLp);
  }
};

代码示例来源:origin: AmazMod/AmazMod

buttonsLayout.setOrientation(LinearLayout.VERTICAL);
replyButton.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
replyButton.setGravity(Gravity.CENTER);

代码示例来源:origin: wasdennnoch/AndroidN-ify

mAlarmStatusCollapsed.setLayoutParams(alarmStatusCollapsedLp);
mAlarmStatusCollapsed.setId(View.generateViewId());
mAlarmStatusCollapsed.setGravity(Gravity.TOP);
mAlarmStatusCollapsed.setTextColor(alarmStatusTextColor);
mAlarmStatusCollapsed.setTextSize(TypedValue.COMPLEX_UNIT_PX, dateTimeCollapsedSize);

代码示例来源:origin: LemonITCN/LemonHello4Android

actionView.setBackgroundColor(action.getBackgroundColor());
actionView.setTextSize(buttonFontSize);
actionView.setGravity(Gravity.CENTER);
actionContainer.addView(actionView);
actionView.setOnClickListener(new View.OnClickListener() {

相关文章

Button类方法