Android的按钮形状绘制保证金 [英] Android button shape drawable margin

查看:321
本文介绍了Android的按钮形状绘制保证金的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林造型使用形状绘制一个按钮,这将导致按钮在扩大很少或失去它的保证金所以它不与其他按键排列好。任何想法,为什么:

这是我的布局code:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:广告=htt​​p://schemas.android.com/apk/lib/com.google.ads
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:ID =@ + ID / LinearLayout1
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
 < TableLayout
    机器人:layout_width =match_parent
    机器人:layout_height =0dip
    机器人:layout_weight =1
    机器人:stretchColumns =*
    机器人:weightSum =2>
<的TableRow
        机器人:ID =@ + ID / tableRowMem
        机器人:layout_weight =1>            <按钮
                机器人:ID =@ + ID /按钮a
                机器人:layout_width =0dip
                机器人:layout_height =match_parent
                机器人:layout_weight =1
                机器人:文字=A/>        <按钮
            机器人:ID =@ + ID / buttonB
            机器人:layout_width =0dp
            机器人:layout_height =match_parent
            机器人:layout_weight =1
            机器人:文字=B/>    < /&的TableRow GT;    <的TableRow
        机器人:ID =@ + ID / tableRow1
        机器人:layout_weight =1>        <按钮
            机器人:ID =@ + ID / buttonC
            机器人:layout_width =0dp
            机器人:layout_weight =1
            机器人:layout_height =match_parent
            机器人:文字=C/>        <按钮
            机器人:ID =@ + ID / buttonD
            机器人:layout_width =0dp
            机器人:layout_weight =1
            机器人:layout_height =match_parent
            机器人:背景=@绘制/ buttonred
            机器人:文字=D/>
    < /&的TableRow GT;
    < / TableLayout>
    < / LinearLayout中>

这是形状绘制:

 <?XML版本=1.0编码=UTF-8&GT?;
<选择机器人:形状=矩形的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目>    <形状和GT;
         <梯度
    机器人:startColor =#FFFF0000
    机器人:ENDCOLOR =#80FF00FF
    机器人:角=45/>    < /形状>
< /项目>
< /选择>


解决方案

看下面的图..默认按键是9片图像所以总是有多余的空间按钮后,这是你的按钮捕获。所以当你设置背景资源的空白被填补的图像。因此,更好地利用的ImageView 来代替。它将与ImageView的正常工作。虽然有可能会像填充解决方案,但他们会在一些situtation失败。可以实现与ImageView的功能。我希望这会帮助你。

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>

解决方案

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.

这篇关于Android的按钮形状绘制保证金的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆