简体   繁体   中英

How to adjust Android button's padding space for text in Java?

My Java code looks like this :

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

The image looks like this :

在此处输入图片说明

As you can see the text is hard to see, yet there are plenty of space around text, in Java Swing, there is "Insets" I can use to adjust spacing around text, in Android how to do it with Java so that I can reduce space around text and make text larger ?

OK, done like this :

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

Looks like this :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM