简体   繁体   English

如何继承Android样式属性?

[英]How to inherit Android style attribute?

I've recently tried adopting a style-based approached to my android app.我最近尝试在我的 android 应用程序中采用基于样式的方法。 However, I'm stuck with this dilemma.但是,我陷入了这个困境。

In my layout file I originally had this:在我的布局文件中,我最初有这个:

 <LinearLayout
         android:id="@+id/layout_event_buttons"
         style="?android:attr/buttonBarStyle"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:orientation="horizontal" >

         <ImageButton
             android:id="@+id/btn_create_event"
             style="?android:attr/buttonBarButtonStyle"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:contentDescription="@string/btn_save"
             android:src="@drawable/content_save"
             android:text="@string/btn_save" />
</LinearLayout>

In my styles.xml I tried doing this with no luck:在我的styles.xml 中,我尝试这样做但没有运气:

<style name="Buttons" parent="?android:attr/buttonBarButtonStyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
</style>

I already tried different variations of the above like:我已经尝试了上述的不同变体,例如:

?android:buttonBarButtonStyle

?attr:buttonBarButtonStyle

@android:attr/buttonBarButtonStyle

@android:buttonBarButtonStyle

I also tried:我也试过:

<item name="style">?android:attr/buttonBarButtonStyle</item> 

to no avail.无济于事。

My app won't compile because it can't find the buttonBarButtonStyle resource.我的应用程序无法编译,因为它找不到 buttonBarButtonStyle 资源。

This is how I applied it.这就是我应用它的方式。 My other styles work fine if I just inherit the common themes like Holo, etc.如果我只是继承 Holo 等常见主题,我的其他风格就可以正常工作。

<ImageButton
     style="@style/Buttons"
     android:id="@+id/btn_create_event"
     android:contentDescription="@string/btn_save"
     android:src="@drawable/content_save"
     android:text="@string/btn_save" />

Found the answer to be similar to this post: How does an android:attr style work?发现答案类似于这篇文章: android:attr 样式如何工作?

Turns out this style is private and you may need to copy it completely to be able to pull it off.原来这种风格是私人的,你可能需要完全复制它才能实现它。 :( :(

I've found that applying android:background="?android:attr/selectableItemBackground" to the button attributes removes the borders, which makes the button act like the android buttonbar (after extending the ?android:attr/buttonBarButtonStyle as you have.我发现将android:background="?android:attr/selectableItemBackground"应用于按钮属性会删除边框,这使得按钮的行为类似于 android 按钮栏(在扩展?android:attr/buttonBarButtonStyle

This post explains it well 这个帖子解释的很好

For AppCompat, you can inherit from the Parent style.对于 AppCompat,您可以从 Parent 样式继承。 For example, add this to each button.例如,将此添加到每个按钮。

<style name="MyBorderlessButtonBarStyle" parent="@style/Widget.AppCompat.Button.Borderless">

    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:layout_weight">1</item>

    <!--more items-->

</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM