安卓自定义按钮边距

27

我有一个自定义的按钮布局

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <gradient android:startColor="@color/pres1"
                android:endColor="@color/pres2" android:angle="270" />
            <stroke android:width="5dp" android:color="@color/stro3" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="20dp"
                android:right="10dp" android:bottom="20dp" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape>
            <gradient android:endColor="@color/focu1"
                android:startColor="@color/focu2" android:angle="270" />
            <stroke android:width="5dp" android:color="@color/stro2" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="20dp"
                android:right="10dp" android:bottom="20dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient android:endColor="@color/norm1"
                android:startColor="@color/norm2" android:angle="270" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="20dp"
                android:right="10dp" android:bottom="20dp" />
        </shape>
    </item>

</selector>

对于下面的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@color/all_white">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello"
        android:textColor="@color/all_red" />

    <Button android:id="@+id/mq_categories" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Browse Quiz Categories"
        android:background="@drawable/custom_button" />
    <Button android:id="@+id/mq_random" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Enter Random Quiz"
        android:background="@drawable/custom_button" />

</LinearLayout>

生成以下输出:

alt text

我需要在按钮之间添加一些边距,任何帮助都将不胜感激。


你好,我正在寻找与这些按钮完全相同的颜色,您能否请提供这些颜色的十六进制代码?谢谢! - idish
3个回答

35

就这样做

<Button android:id="@+id/mq_categories"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Browse Quiz Categories"
    android:background="@drawable/custom_button"
    android:layout_marginBottom="5dp"/>

通过这种方式,在你的第一个按钮底部设置了5个密度无关像素的边距。


1
是啊,但为什么边距会消失呢?即使在自定义按钮样式中指定了layout_margin,它似乎也被忽略了! - HRJ
8
这意味着我必须在所有的按钮视图上设置边距(或填充)。这能通过主题来完成吗?(而不必在应用程序中为所有视图设置填充?http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme) - Diederik

2

我曾经遇到过同样的问题,但似乎没有找到解决方案。在尝试了几个方法之后,以下方法对我起了作用:

  1. Define Custom Button Layout as done in the question
  2. Inside res>values>styles.xml file, add the new style as below,

    <style name="Widget.Button_Custom" parent="android:Widget">
        <item name="android:layout_margin">5dp</item>
        <item name="android:background">@drawable/button_custom</item>
    </style>
    
  3. Use the style in your layout

,

<?xml version="1.0" encoding="utf-8"?> 
<Button android:id="@+id/mq_categories" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="
        android:background="@drawable/custom_button" 
        style="@style/Widget.Button_Custom"
/>

应该就这样了。除了margin之外,您还可以在style标签中定义许多其他属性。希望这有所帮助!


1

建议使用RelativeLayout而不是LinearLayout,这样可以设置所有可能的边距。谢谢。


请您能解释一下您的意思吗?我真的不太理解。 - Octavian Helm
在布局文件main.xml中,当您将其填充到UI中时,应该有一个LinearLayout。您可以在其中使用RelativeLayout,并通过将TextView保持为根来设置按钮的边距和位置。 - Ashay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接