Reputation: 12656
I have 4 image+text buttons arranged in a 4x4 grid. I'm using a with the android:drawableTop attribute. The button background is a state list drawable. They are children of two nested LinearLayouts that arrange them in a 2x2 grid.
My problem is, for aesthetic reasons, I want the buttons to be exactly square. How do I do this? On some devices they are stretched tall and on others they are squashed flat. Is there any way to instruct Android to make the height and width always equal regardless of the screen dimensions or orientation?
If it's not asking too much, it would be great if this could be done via XML without custom Java.
Thanks in advance...
Button:
<Button
android:id="@+id/btn_help_support"
style="@style/btn_home"
android:drawableTop="@drawable/home_btn_help_support"
android:text="@string/home_btn_help_support" />
Button style:
<style name="btn_home">
<item name="android:gravity">center</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_weight">1</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:background">@drawable/btn_std</item>
<item name="android:drawablePadding">10dp</item>
<item name="android:textColor">@color/btn_std_text</item>
<item name="android:textSize">@dimen/home_btn_font_size</item>
</style>
Upvotes: 0
Views: 1444
Reputation: 5739
I believe this is because your layout_width
and layout_height
are set to fill_parent
and wrap_content
respectively.
Now, it may be tricky to account for different screen sizes, so hard-coding an integer value may not work, but it's worth a shot. Otherwise, you'll have to omit these from the style and define them in Java.
Upvotes: 1