如何创建完全填充屏幕宽度的方形按钮

How to create exactly square buttons that fill in width of screen

我有一个 activity,它根据 TableLayout 和 TableRow 动态填充一些按钮,如下所示:

    private TableLayout buttonTableLayout;
    //-----
    for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
        ((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    for (int row = 0; row < 5; row++) {
        TableRow currentTableRow = getTableRow(row);

        for (int column = 0; column < 5; column++) {
                Button newGuessButton = (Button) inflater.inflate(R.layout.my_button, currentTableRow, false);
                newGuessButton.setText(String.valueOf((row * 5) + column + 1));
                currentTableRow.addView(newGuessButton);
            }

        }
    }
    //----
    private TableRow getTableRow(int row) {
        return (TableRow) buttonTableLayout.getChildAt(row);
    }

我想制作一个 5*5 的按钮列表,其中 1:All 个具有相同的宽度和高度,并且 2:让它们填满屏幕。 在我的代码中,我有一个名为 my_button 的布局,例如:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newButton"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/button_style"></Button>

<Button 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="1"
android:background="@drawable/button_style"></Button>

结果是:

我已经改变了重力,但它 works.Is 没有任何方法可以使它们完全正方形并填充屏幕的宽度。

您应该子class Button 查看并覆盖以下功能:

public class SquareButton extends Button {

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

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

    public SquareButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SquareButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

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

}

编辑:好的,您需要像上面那样自定义您的按钮。然后,您可以使用上面的 SquareButton 而不是使用默认按钮。

 <com.kingfisher.utils.SquareButton
            android:id="@+id/btnSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="100"
            android:text="Downloaded"/>

你必须像这样动态获取屏幕的宽度:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

得到这个之后,你必须将宽度除以 5,这样你就可以根据屏幕的宽度得到一个按钮的宽度。

int buttonWidthAndHeight = width/ 5;

现在,当您将按钮添加到 table 布局时,请像这样设置按钮的宽度和高度:

 TableLayout.LayoutParams lp= new TableLayout.LayaoutParams(buttonWidthAndHeight ,buttonWidthAndHeight ); // width,height
 newGuessButton.setLayoutParams(lp);

快乐编码!!!!!

请尝试此代码,将 button_rect_shape.xml 放在可绘制文件夹中。

button_rect_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
     />


<solid android:color="#fc5117"/>
    <!--<stroke
        android:width="1dp"
        android:color="#ffffff" />
-->

    <!--<gradient android:angle="270"
        android:centerColor="#fc5117"
        android:endColor="#fc5117"
        android:startColor="#fc5117" />-->
</shape>

SampleLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">



    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="100dp"


        android:layout_gravity="center_horizontal"
        android:background="@drawable/button_rect_shape"
        />



</LinearLayout>