Android:如何在styles.xml中自定义声明XML命名空间?

mel*_*kim 17 xml layout android android-layout android-styles

我正在尝试将自定义XML命名空间放入其中styles.xml并在布局中继承它.我不知道如何styles.xml在布局xml(例如xmlns:app="http://schemas.android.com/tools")中声明自定义XML命名空间.

我如何在styles.xml?中使用自定义XML命名空间?

是)我有的:

  1. 字体资产,ReallyCoolFont.ttf保存在asset/fonts.

  2. my_layout.xml:

    <TextView
        <!-- more attributes here -->
        app:customFont="fonts/ReallyCoolFont.ttf" 
        <!-- more attributes here -->
    </TextView>
    
    Run Code Online (Sandbox Code Playgroud)
  3. styles.xml:

    <style name="CoolTextView">
        <!-- more items here -->
        <!-- more items here -->
    </style>
    
    Run Code Online (Sandbox Code Playgroud)

我想要的是:

  1. my_layout.xml:

    <TextView
        <!-- more attributes here -->
        style="@style/CoolTextView
        <!-- more attributes here -->
    </TextView>
    
    Run Code Online (Sandbox Code Playgroud)
  2. styles.xml:

    <style name="CoolTextView">
        <!-- more items here -->
        <item name="app:customFont">ReallyCoolFont.ttf</item>
        <!-- more items here -->
    </style>
    
    Run Code Online (Sandbox Code Playgroud)

我得到的错误:

Error:(1403, 21) No resource found that matches the given name: attr     'app:customFont'.
Run Code Online (Sandbox Code Playgroud)

mml*_*loo 16

1)您需要在res文件夹中的attr.xml文件中为您的字体定义一个属性:

<attr name="myfonts" format="string"></attr>
Run Code Online (Sandbox Code Playgroud)

2)您需要为TextView定义自定义样式,在这里我们使用我们定义的属性(myfonts):

<declare-styleable name="MyCustomStyle">
    <attr name="myfonts" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

3)

<style name="CoolTextView">
    <item name="myfonts">ReallyCoolFont.ttf</item>
</style>
Run Code Online (Sandbox Code Playgroud)

到目前为止你的总结:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="myfonts" format="string">
    </attr>

    <declare-styleable name="MyCustomStyle">
        <attr name="myfonts" />
    </declare-styleable> 

    <style name="CoolTextView">
        <item name="myfonts">ReallyCoolFont.ttf</item>
    </style>

</resources> 
Run Code Online (Sandbox Code Playgroud)

4)现在您的布局将是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.MyCustomTextView
        android:id="@+id/result"
        style="@style/CoolTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="HELLO WORLD!"
        android:textSize="24dp"
        android:gravity="center" >
    </com.example.MyCustomTextView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

5)和你的MyCustomTextView是:

public class MyCustomTextView extends TextView {


    private static final String TAG = "TextView";

    public MyCustomTextView(Context context) {
        super(context);
    }

    public MyCustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        settingFont(context, attrs);
    }

    public MyCustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        settingFont(context, attrs);
    }


    private void settingFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.MyCustomStyle);
        String customFont = a.getString(R.styleable.MyCustomStyle_myfonts);
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), customFont);  
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
            a.recycle();
            return;
        }

        setTypeface(tf);  
        a.recycle();
    }


}
Run Code Online (Sandbox Code Playgroud)

我假设您将字体放在资产中而不是在资产/字体目录中.

我也强烈推荐阅读本文.


iva*_*arz 11

您无需添加任何前缀来引用样式资源文件中的自定义属性.这样做会很好:

<style name="CoolTextView">
    <item name="customFont">ReallyCoolFont.ttf</item>
</style>
Run Code Online (Sandbox Code Playgroud)


Boj*_*man 5

您不需要任何前缀,没有前缀也可以使用。这是我的一个项目的代码,效果很好

<style name="defaultTriangle">
    <item name="triangleColor">#FF33B5E5</item>
    <item name="triangleStrokeColor">@android:color/black</item>
    <item name="triangleStrokeWidth">3dp</item>
</style>


<si.kseneman.views.Triangle
    style="@style/defaultTriangle"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:padding="10dp"
    android:rotation="0"
    />
Run Code Online (Sandbox Code Playgroud)


Yac*_*oza 5

答案是不要在样式中指定名称空间。

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:custom="http://schemas.android.com/apk/res/com.custom.project">
    <style name="CustomStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>

        <item name="customAttr">value</item> <!-- tee hee -->
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)


kri*_*son 0

<declare-styleable>自定义属性使用标签定义;通常该文件名为 attrs.xml。命名空间声明中将包含您的应用程序的包名称。

整个过程如下所述:创建视图类 | 安卓开发者