android - How to create exactly square buttons that fill in width of screen -
i've activity fill buttons dynamically base on tablelayout , tablerow this:
private tablelayout buttontablelayout; //----- (int row = 0; row < buttontablelayout.getchildcount(); ++row) ((tablerow) buttontablelayout.getchildat(row)).removeallviews(); layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); (int row = 0; row < 5; row++) { tablerow currenttablerow = gettablerow(row); (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 make 5*5 list of buttons 1:all of them has same width , height , 2: make them fill of screen. in code have layout named 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>
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>
i have change gravity doesn't works.is there way make them square , fill width of screen.
you should sub-class button
view , 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 square } }
edit: ok, need customize button above. then, instead of using default button, can use 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"/>
Comments
Post a Comment