【问题标题】:Use custom button as parent in custom style在自定义样式中使用自定义按钮作为父级
【发布时间】:2014-06-20 22:05:43
【问题描述】:

在我的项目中,我为按钮使用特殊字体。所以我添加了PixlUI 库,这样我就可以在 xml 中设置字体了。

<com.neopixl.pixlui.components.button.Button
            android:id="@+id/btn_login"
            style="@style/custom_button_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/login"
            pixlui:typeface="AvenirNextCondensed-Regular.ttf" />


        <com.neopixl.pixlui.components.button.Button
            android:id="@+id/btn_register"
            style="@style/custom_button_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/make_new_profile"
            pixlui:typeface="AvenirNextCondensed-Regular.ttf" />


        <com.neopixl.pixlui.components.button.Button
            android:id="@+id/btn_broker_register"
            style="@style/custom_button_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/make_new_profile_broker"
            pixlui:typeface="AvenirNextCondensed-Regular.ttf" />

这些是我的按钮,因为它们都具有相同的字体,我想在“custom_button_style”中包含该字体

这是我的自定义样式:

<style name="custom_button_style" parent="@android:style/Widget.Button">
    <item name="android:textSize">@dimen/hello_medium_fontsize</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@drawable/custom_btn_background</item>
    <item name="android:layout_marginBottom">@dimen/login_button_margin</item>
</style>

如何添加

pixlui:字体

这种风格?

【问题讨论】:

  • &lt;item name="pixlui:typeface"&gt;AvenirNextCondensed-Regular.ttf&lt;/item&gt; 有效吗?
  • 不,我收到错误消息“找不到与给定名称匹配的资源:attr 'pixlui:typeface'。我必须改变父母吗?
  • 您是否将此属性设置为您的父布局?:xmlns:pixlui="schemas.android.com/apk/com.neopixl.pixlui"。通常,你可以使用 pixlui 属性
  • @Opiatefuchs 我已将该属性设置为我的父布局,并且在我的布局中使用该属性没有问题。但我想在我的样式中包含该属性,以避免每个按钮都包含它。
  • 这个有运气吗?

标签: android android-styles


【解决方案1】:

不清楚我们是否看不到完整的 xml 布局文件,但经常发生的事情是人们忘记将 xmlns 属性添加到父级。你的父布局应该是这样的。它是这个库的一部分,重要的是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" <----thats the important attribute
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

您不需要在您的 style.xml 中添加任何内容。因此,如果这不能解决您的问题,请返回。

编辑

它还没有经过测试,但它可能是:

  <?xml version="1.0" encoding="utf-8" ?>
   <resources xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui">
     <style "custom_button_style" parent="@android:style/Widget.Button">
    <item name="android:textSize">@dimen/hello_medium_fontsize</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@drawable/custom_btn_background</item>
    <item name="android:layout_marginBottom">@dimen/login_button_margin</item>
    <item name="pixlui:typeface">AvenirNextCondensed-Regular.ttf</item> 
 </style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 2015-10-04
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多