我创建了此可绘制对象以在应用程序的按钮中使用:
btnprimary.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btnprimary_disabled" android:state_enabled="false" />
<item android:drawable="@drawable/btnprimary_pressed" android:state_enabled="true" android:state_pressed="true" />
<item android:drawable="@drawable/btnprimary_focused" android:state_enabled="true" android:state_focused="true" />
<item android:drawable="@drawable/btnprimary_enabled" android:state_enabled="true" />
</selector>
每个州也都有一个
drawable
,因此,例如,这是btnprimary_enabled
的文件<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="5dp"
/>
<gradient
android:angle="90"
android:startColor="#335BB8"
android:endColor="#6397FF"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="1dp"
android:color="#878787"
/>
</shape>
因此,我可以使用background属性在这样的按钮中使用它:
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btnprimary"
android:text="@string/login"
android:textColor="#FFFFFF"
android:gravity="center"/>
我在
styles.xml
上有主题,但是现在是空的。像这样:<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
我的问题是,是否可以将在
drawable
上创建的那些配置放入主题中,并且所有按钮都具有这种样式,而无需添加background
属性? 最佳答案
您是否尝试过此代码?在样式文件中,定义按钮样式
<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">@style/yourButton</item>
</style>