如何为 Java 中的文本调整 Android 按钮的内边距 space?
How to adjust Android button's padding space for text in Java?
我的 Java 代码如下所示:
LinearLayout symbolsTableLayout = new LinearLayout(context);
symbolsTableLayout.setId(View.generateViewId());
symbolsTableLayout.setBackgroundColor(Color.rgb(198,218,250));
symbolsTableLayout.setHorizontalGravity(Gravity.CENTER);
symbolsTableLayout.setVerticalGravity(Gravity.CENTER);
addView(symbolsTableLayout, LinearLayout.LayoutParams.MATCH_PARENT,460);
GridLayout tableLayout = new GridLayout(context);
tableLayout.setColumnCount(col_count);
tableLayout.setRowCount(row_count);
// tableLayout.setBackgroundColor(Color.rgb(198,128,230)); // Color.rgb(198,128,230) pink
symbolsTableLayout.addView(tableLayout, 1100,400);
symbolButton = new Button[row_count][col_count];
for (int row = 0; row < row_count; row++)
for (int col = 0; col < col_count; col++)
{
symbolButton[row][col] = new Button(context);
symbolButton[row][col].setText(row*col_count+col+1+"");
symbolButton[row][col].setTypeface(Typeface.MONOSPACE);
symbolButton[row][col].setAllCaps(false);
symbolButton[row][col].setTextSize(6);
symbolButton[row][col].setOnClickListener(Symbol_Button_Listener);
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setCornerRadii(new float[]{8, 8, 8, 8, 8, 8, 8, 8});
drawable.setStroke(3, Color.rgb(153, 187, 255));
drawable.setColor(Color.rgb(214, 215, 215));
symbolButton[row][col].setBackground(drawable);
tableLayout.addView(symbolButton[row][col], 44, 50);
}
图像看起来像这样:
如你所见,文字很难看清,但文字周围有很多 space,在 Java Swing 中,有 "Insets" 我可以用来调整间距文本周围,在 Android 如何使用 Java 来减少文本周围的 space 并使文本变大 ?
好的,这样就完成了:
LinearLayout symbolsTableLayout = new LinearLayout(context);
symbolsTableLayout.setId(View.generateViewId());
symbolsTableLayout.setBackgroundColor(Color.rgb(198,18,250));
symbolsTableLayout.setHorizontalGravity(Gravity.CENTER);
symbolsTableLayout.setVerticalGravity(Gravity.CENTER);
addView(symbolsTableLayout, LinearLayout.LayoutParams.MATCH_PARENT,488);
GridLayout tableLayout = new GridLayout(context);
tableLayout.setColumnCount(col_count);
tableLayout.setRowCount(row_count);
// tableLayout.setBackgroundColor(Color.rgb(198,128,230)); // Color.rgb(198,128,230) pink
symbolsTableLayout.addView(tableLayout, 1100,398);
symbolButton = new Button[row_count][col_count];
for (int row = 0; row < row_count; row++)
for (int col = 0; col < col_count; col++)
{
symbolButton[row][col] = new Button(context);
symbolButton[row][col].setText(row*col_count+col+1+"");
symbolButton[row][col].setTypeface(Typeface.MONOSPACE);
symbolButton[row][col].setAllCaps(false);
symbolButton[row][col].setTextSize(12);
symbolButton[row][col].setOnClickListener(Symbol_Button_Listener);
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setCornerRadii(new float[]{6, 6, 6, 6, 6, 6, 6, 6});
drawable.setStroke(3, Color.rgb(153, 187, 255));
drawable.setColor(Color.rgb(214, 215, 215));
symbolButton[row][col].setBackground(drawable);
symbolButton[row][col].setPadding(0,0,0,0);
tableLayout.addView(symbolButton[row][col], 44, 50);
}
看起来像这样:
我的 Java 代码如下所示:
LinearLayout symbolsTableLayout = new LinearLayout(context);
symbolsTableLayout.setId(View.generateViewId());
symbolsTableLayout.setBackgroundColor(Color.rgb(198,218,250));
symbolsTableLayout.setHorizontalGravity(Gravity.CENTER);
symbolsTableLayout.setVerticalGravity(Gravity.CENTER);
addView(symbolsTableLayout, LinearLayout.LayoutParams.MATCH_PARENT,460);
GridLayout tableLayout = new GridLayout(context);
tableLayout.setColumnCount(col_count);
tableLayout.setRowCount(row_count);
// tableLayout.setBackgroundColor(Color.rgb(198,128,230)); // Color.rgb(198,128,230) pink
symbolsTableLayout.addView(tableLayout, 1100,400);
symbolButton = new Button[row_count][col_count];
for (int row = 0; row < row_count; row++)
for (int col = 0; col < col_count; col++)
{
symbolButton[row][col] = new Button(context);
symbolButton[row][col].setText(row*col_count+col+1+"");
symbolButton[row][col].setTypeface(Typeface.MONOSPACE);
symbolButton[row][col].setAllCaps(false);
symbolButton[row][col].setTextSize(6);
symbolButton[row][col].setOnClickListener(Symbol_Button_Listener);
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setCornerRadii(new float[]{8, 8, 8, 8, 8, 8, 8, 8});
drawable.setStroke(3, Color.rgb(153, 187, 255));
drawable.setColor(Color.rgb(214, 215, 215));
symbolButton[row][col].setBackground(drawable);
tableLayout.addView(symbolButton[row][col], 44, 50);
}
图像看起来像这样:
如你所见,文字很难看清,但文字周围有很多 space,在 Java Swing 中,有 "Insets" 我可以用来调整间距文本周围,在 Android 如何使用 Java 来减少文本周围的 space 并使文本变大 ?
好的,这样就完成了:
LinearLayout symbolsTableLayout = new LinearLayout(context);
symbolsTableLayout.setId(View.generateViewId());
symbolsTableLayout.setBackgroundColor(Color.rgb(198,18,250));
symbolsTableLayout.setHorizontalGravity(Gravity.CENTER);
symbolsTableLayout.setVerticalGravity(Gravity.CENTER);
addView(symbolsTableLayout, LinearLayout.LayoutParams.MATCH_PARENT,488);
GridLayout tableLayout = new GridLayout(context);
tableLayout.setColumnCount(col_count);
tableLayout.setRowCount(row_count);
// tableLayout.setBackgroundColor(Color.rgb(198,128,230)); // Color.rgb(198,128,230) pink
symbolsTableLayout.addView(tableLayout, 1100,398);
symbolButton = new Button[row_count][col_count];
for (int row = 0; row < row_count; row++)
for (int col = 0; col < col_count; col++)
{
symbolButton[row][col] = new Button(context);
symbolButton[row][col].setText(row*col_count+col+1+"");
symbolButton[row][col].setTypeface(Typeface.MONOSPACE);
symbolButton[row][col].setAllCaps(false);
symbolButton[row][col].setTextSize(12);
symbolButton[row][col].setOnClickListener(Symbol_Button_Listener);
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setCornerRadii(new float[]{6, 6, 6, 6, 6, 6, 6, 6});
drawable.setStroke(3, Color.rgb(153, 187, 255));
drawable.setColor(Color.rgb(214, 215, 215));
symbolButton[row][col].setBackground(drawable);
symbolButton[row][col].setPadding(0,0,0,0);
tableLayout.addView(symbolButton[row][col], 44, 50);
}
看起来像这样: