简体   繁体   中英

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

I've an activity that fill with some buttons dynamically base on TableLayout and TableRow like this:

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

I want to make a 5*5 list of buttons that 1:All of them has the same width and height and 2: make them fill of screen. In my code I have a layout that named my_button, like :

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

or

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

and results is: 在此处输入图片说明 在此处输入图片说明

I have change the gravity but it doesn't works.Is there any way to make them exactly square and fill the width of screen.

You should sub-class Button view and override below function:

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
    }

}

Edit: Ok, you need to customize your button as above. Then, instead of using the default button, you can use the above 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"/>

Please Try this code, place the button_rect_shape.xml on drawable folder.

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>

you have to dynamically get the width of screen like this:

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

And after getting this you have to divide the width by 5 so that you will be having width of one button on the basis of width of screen.

int buttonWidthAndHeight = width/ 5;

Now when you are adding button to your table layout set the width and height of button as well like this:

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

Happy Coding!!!!!

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