如何继承Android的样式属性?

9

我最近尝试采用基于样式的方法来开发我的安卓应用。然而,我遇到了一个困境。

在我的布局文件中,我最初是这样写的:

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

在我的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>
我已经尝试了不同的变体,例如: ?android:buttonBarButtonStyle ?attr:buttonBarButtonStyle @android:attr/buttonBarButtonStyle @android:buttonBarButtonStyle 我还尝试过:
<item name="style">?android:attr/buttonBarButtonStyle</item> 

无济于事。

我的应用程序因为找不到buttonBarButtonStyle资源而无法编译。

这是我使用它的方式。如果我只继承常见的主题如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" />

顺便说一下,我的清单文件中有这个: <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="18" /> 所以我认为我没有在不支持的 API 级别上运行它。 - Chad
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
6

你不能拥有超过一个同名的自定义属性。 - TheLibrarian

0
我发现将 android:background="?android:attr/selectableItemBackground" 应用于按钮属性可以去除边框,这使得按钮的行为类似于 Android 按钮栏(在扩展 ?android:attr/buttonBarButtonStyle 之后)。 这篇帖子阐述得很清楚。

0

对于AppCompat,您可以继承父样式。例如,将此添加到每个按钮。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,