Android按钮高度(以编程方式)

inc*_*inc 1 layout android button

如何设置按钮高度小于30px?我尝试了下一个:


在此输入图像描述


  1. 自动调用setHeight(30); (设置150正在工作,但设置30不工作)
  2. setPadding(0,-20,0,-20); (宽度是变化,高度不是)
  3. LinearLayout.LayoutParams(设置150工作,但设置30不工作)
  4. new Button(this,null,android.R.attr.buttonStyleSmall); - 仅对按钮文本有效

在此输入图像描述


Button mainButton = new Button(this);
mainButton.setPadding(0, 0, 0, 0);
mainButton.setLayoutParams(new LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT,FlowLayout.LayoutParams.WRAP_CONTENT));
l.addView(mainButton);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


SmallButton mainButton = new SmallButton(this);
        mainButton.setText(s);
        mainButton.setBackgroundResource(R.drawable.button_drawable);
        mainButton.setLayoutParams(new LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT,FlowLayout.LayoutParams.WRAP_CONTENT));
        l.addView(mainButton);

public class SmallButton extends Button {

public SmallButton(Context context) {
    super(context);
}

public SmallButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SmallButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     super.onMeasure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY, 35));
}

 @Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    canvas.translate(0,-5);
    super.onDraw(canvas);
    canvas.restore();
}
Run Code Online (Sandbox Code Playgroud)

}

在此输入图像描述

我有一个可绘制的Shape用于我的背景渐变,因为我不知道确切的宽度.我也尝试设置它的高度.是否可以将按钮包装成文本?

编辑: 使用TextView而不是Button解决了这个问题.当然,如果你没有使用Button.class中的特定/覆盖的东西.

小智 8

有两个参数.Button.setMinimumHeight()和Button.setMinHeight().确保将它们都设置为值"0".