【发布时间】:2016-06-23 08:11:26
【问题描述】:
我的活动中有一些按钮从自定义类中膨胀,这些按钮使按钮完全正方形并适合它们在父项中取决于屏幕大小并且它工作正常:
public class MyButton extends Button {
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size); // make it square
}
}
这里是我的充气机:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Button newGuessButton = (Button) inflater.inflate(R.layout.my_button, currentTableRow, false);
currentView.addView(newGuessButton);
和 layout.xml:
<my.package.name.MyButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="100"/>
我的问题是如何在我的按钮中设置文本大小以根据需要更改默认大小。我想我必须在将其添加为视图之前对其进行设置,但我不知道如何?
【问题讨论】:
标签: android android-layout android-custom-view android-button