自定义按钮中的填充不起作用

时间:2015-08-09 20:57:07

标签: android xml

我有按钮样式:

   <style name="NavDrawerButton" parent="android:Widget.Button">
        <item name="android:background">@drawable/navdrawer_item</item>
        <item name="android:textColor">@color/navdrawer_item_text</item>
        <item name="android:layout_marginTop">1dp</item>
        <item name="android:gravity">start|center_vertical</item>
        <item name="android:paddingLeft">16dp</item>
        <item name="android:paddingRight">16dp</item>
        <item name="android:textSize">16sp</item>
        <item name="android:clickable">true</item>
    </style>

In / drawable:

<!--Navdrawer_item.xml-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/navdrawer_item_pressed"/>
    <item
        android:state_enabled="true"
        android:drawable="@drawable/navdrawer_item_enabled" />
</selector>

<!--navdrawer_item_enabled-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/backrepeat">
    </item>

</layer-list>

我的按钮:

<Button
                    android:id="@+id/category1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    style="@style/NavDrawerButton"
                    android:text="@string/category1"/>

问题是填充不起作用。按钮的左边缘和文本内部没有填充。我无法理解为什么会如此。 你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我测试了你的代码没有错误......可能在你的父布局......

string.xml

 <style name="NavDrawerButton" parent="android:Widget.Button">
    <item name="android:background">@drawable/image1</item>
    <item name="android:layout_marginTop">1dp</item>
    <item name="android:gravity">start|center_vertical</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:textSize">16sp</item>
    <item name="android:clickable">true</item>
</style>

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.MainActivity"
 android:orientation="vertical" >

  <Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:text="sdfg"
    style="@style/NavDrawerButton" >
  </Button>
</LinearLayout>

输出

enter image description here