
actionbar_custom_vIEw_done_discard.xml
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="horizontal"> <include layout="@layout/actionbar_discard_button" /> <VIEw androID:layout_wIDth="1px" androID:layout_height="match_parent" androID:layout_margintop="12dp" androID:layout_marginBottom="12dp" androID:background="?attr/actionbarCustomVIEwdivIDercolor" /> <include layout="@layout/actionbar_done_button" /></linearLayout>
actionbar_done_button.xml
<FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/actionbar_done" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:paddingRight="20dp" androID:drawableleft="?attr/actionbarDoneIcon" androID:drawablepadding="8dp" androID:gravity="center_vertical" androID:text="@string/save" /></FrameLayout>
StockAlertFragmentActivity.java
public class StockAlertFragmentActivity extends SherlockFragmentActivity { @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Inflate a "Done/discard" custom action bar vIEw. LayoutInflater inflater = (LayoutInflater) this.getSupportActionbar().getthemedContext() .getSystemService(LAYOUT_INFLATER_SERVICE); final VIEw customActionbarVIEw = inflater.inflate( R.layout.actionbar_custom_vIEw_done_discard,null); 当我们点击SAVE按钮时,结果如下.
但是,在将我们的应用程序迁移到材料设计的应用程序(targetSdkVersion 23,主题theme.AppCompat.light.NoActionbar)后,系统属性attr / actionbuttonStyle不再适用于矩形按钮.
相反,它在矩形按钮的顶部绘制一个圆形覆盖.
toolbar_with_save_discard.xml
<androID.support.v7.Widget.Toolbar xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:ID="@+ID/toolbar" androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:minHeight="?attr/actionbarSize" androID:background="?attr/colorPrimary" androID:elevation="4dp" androID:contentInsetleft="0dp" androID:contentInsetStart="0dp" app:contentInsetleft="0dp" app:contentInsetStart="0dp" androID:contentInsetRight="0dp" androID:contentInsetEnd="0dp" app:contentInsetRight="0dp" app:contentInsetEnd="0dp" app:theme="@style/themeOverlay.AppCompat.Dark.Actionbar" app:popuptheme="@style/themeOverlay.AppCompat.light" > <!-- androID:elevation="4dp" is used due to http://www.Google.com/design/spec/what-is-material/elevation-shadows.HTML#elevation-shadows-elevation-androID- --> <linearLayout androID:ID="@+ID/buttons_linear_layout" androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize" androID:orIEntation="horizontal"> <include layout="@layout/toolbar_discard_button" /> <VIEw androID:layout_wIDth="1px" androID:layout_height="match_parent" androID:layout_margintop="12dp" androID:layout_marginBottom="12dp" androID:background="?attr/toolbarCustomVIEwdivIDercolor" /> <include layout="@layout/toolbar_save_button" /> </linearLayout></androID.support.v7.Widget.Toolbar>
toolbar_save_button.xml
<FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/toolbar_save" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:paddingRight="20dp" androID:drawableleft="?attr/toolbarDoneIcon" androID:drawablepadding="8dp" androID:gravity="center_vertical" androID:text="@string/save" /></FrameLayout>
我喜欢在整个矩形SAVE按钮上绘制叠加层.
我想我最有可能通过避免使用style =“?attr / actionbuttonStyle”并提供我自己定义的选择器来解决这个问题.
但是,我不想这样做.我更喜欢使用系统提供的样式,以减少维护头痛.
如何使style =“?attr / actionbuttonStyle”适用于矩形工具栏按钮?
或者,是否有任何其他系统样式属性,这将适用于矩形工具栏按钮?同时,有涟漪效应.
解决方法 这里的属性是:actionbuttonStyle,你在包装FrameLayout上设置.在第23节,它指向:<style name="Widget.Material.Actionbutton"> <item name="background">?attr/actionbarItemBackground</item> <item name="paddingStart">12dp</item> <item name="paddingEnd">12dp</item> <item name="minWIDth">@dimen/action_button_min_wIDth_material</item> <item name="minHeight">@dimen/action_button_min_height_material</item> <item name="gravity">center</item> <item name="scaleType">center</item> <item name="maxlines">2</item></style>
后台设置为actionbarItemBackground,后者又指向此rippledrawable:
<ripple xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:color="?attr/colorControlHighlight" androID:radius="20dp" />
半径值是您不想要的.
要解决此问题,请在应用主题下覆盖actionbarItemBackground:
<style name="Apptheme" parent="...."> .... <item name="actionbarItemBackground">@drawable/custom_action_bar_item_bg</item> <item name="androID:actionbarItemBackground">@drawable/custom_action_bar_item_bg</item></style>
drawable custom_action_bar_item_bg将定义为:
RES /抽拉/ custom_action_bar_item_bg.xml
<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:state_pressed="true"> <color androID:color="#...."/> </item> <item> <color androID:color="@androID:color/transparent"/> </item></selector>
RES /抽拉-V21 / custom_action_bar_item_bg.xml
<ripple xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:color="?attr/colorControlHighlight"> <item androID:ID="@ID/mask"> <color androID:color="@androID:color/white" /> </item></ripple>
在API版本>的情况下,掩码将为您提供矩形. 21.
缺点:使用actionbarItemBackground的所有项目都将受到影响(除非这没关系).要解决此问题,您可以在FrameLayout包装器上创建单独的主题.例如,这是您的应用程序的主题:
<style name="Apptheme" parent="...."> .... ....</style>
创建从Apptheme继承的另一个主题:
<style name="SelectiveActionbarbuttontheme" parent="Apptheme"> <item name="actionbarItemBackground">@drawable/custom_action_bar_item_bg</item> <item name="androID:actionbarItemBackground">@drawable/custom_action_bar_item_bg</item></style>
要使用它,请在包装器FrameLayout上覆盖androID:theme:
<FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/toolbar_save" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:theme="@style/SelectiveActionbarbuttontheme"> .... ....</FrameLayout>
当然,最简单的方法是将FrameLayout的androID:background设置为?attr / selectableItemBackground,并完成它.
总结以上是内存溢出为你收集整理的android – 如何使style =“?attr / actionButtonStyle”适用于矩形工具栏按钮全部内容,希望文章能够帮你解决android – 如何使style =“?attr / actionButtonStyle”适用于矩形工具栏按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)