Android自定义按钮边距

Android自定义按钮边距,android,android-layout,Android,Android Layout,我有一个自定义按钮布局 <?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="@col

我有一个自定义按钮布局

<?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>

生成以下输出


我需要在按钮之间添加一些边距,欢迎任何帮助。

而不是使用
LinearLayout
使用
RelativeLayout
,这样边距可以按照您所能的方式设置。干杯就这样吧

<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个与密度无关的像素的边距。

我也遇到了同样的问题,解决方案似乎不存在。在尝试了以下几件事情后,我成功了

  • 按照问题中的说明定义自定义按钮布局
  • 在res>values>styles.xml文件中,添加新样式,如下所示

    <style name="Widget.Button_Custom" parent="android:Widget">
        <item name="android:layout_margin">5dp</item>
        <item name="android:background">@drawable/button_custom</item>
    </style>
    
    
    5dp
    @可拉拔/按钮\定制
    
  • 在布局中使用样式

  • ,

    
    
    请你解释一下你的意思好吗?我真的不明白。当你在UI中膨胀它时,在Layout main.xml中。应该有直线布局。在那里,你可以使用RalativeLayout&通过将的文本视图保留为root来设置按钮的边距和位置。是的,但是边距为什么会消失呢?即使在自定义按钮样式中指定了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"
    />