问题
Im styling a button using a shape drawable, this causes the button to over expand a little or lose it's margin so it doesnt align well with the other buttons. any idea why:
This is my layout code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:stretchColumns="*"
android:weightSum="2" >
<TableRow
android:id="@+id/tableRowMem"
android:layout_weight="1" >
<Button
android:id="@+id/buttonA"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A"/>
<Button
android:id="@+id/buttonB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="B" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_weight="1" >
<Button
android:id="@+id/buttonC"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="C" />
<Button
android:id="@+id/buttonD"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@drawable/buttonred"
android:text="D" />
</TableRow>
</TableLayout>
</LinearLayout>
And this is the shape drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
</shape>
</item>
</selector>
回答1:
Look at the below figure.. The default button is 9 patch image so there is always extra space after the button, This is the capture of your button. So when you set the background resource the empty space is filled by the image. So Better use ImageView
instead. It will work perfectly with the imageview. Although there might be solutions like padding but they will fail in some situtation. You can implement the functionality with the ImageView. I hope this will help you.
回答2:
Seems like each button has some dp of padding or layout marging.
Try to set
android:padding=0
If that does not work
android:layout_margin=0
This will eliminate the margin from the default buttons, if you want to solve the other way, add margin to the custom button
回答3:
That probably is because the canvas of the other drawables might have a padding.
Check the canvas padding[not the one you set in xml but the one that designers set in the image itself] and match it with your new drawable and see if they are the same. They should be the same.
回答4:
Try with my below layout i have done some relevant changes:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableLayout android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:stretchColumns="*" android:weightSum="2" > <TableRow android:id="@+id/tableRowMem" android:weightSum="1" > <Button android:id="@+id/buttonA" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="A"/> <Button android:id="@+id/buttonB" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="B" /> </TableRow> <TableRow android:id="@+id/tableRow1" android:weightSum="1" > <Button android:id="@+id/buttonC" android:layout_width="0dp" android:layout_weight=".5" android:layout_height="fill_parent" android:text="C" /> <Button android:id="@+id/buttonD" android:layout_width="0dp" android:layout_weight="0.5" android:layout_height="fill_parent" android:background="@drawable/buttonred" android:text="D" /> </TableRow> </TableLayout> </LinearLayout>
回答5:
Add padding to button.
android:padding="0dp"
来源:https://stackoverflow.com/questions/15037895/android-button-shape-drawable-margin