Android 自定義命名空間

Android 自定義命名空間

  • 聲明規則
  • 屬性定義
  • 示例

聲明規則

一般是在佈局文件的頭部去聲明

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:hiddenLine = "http://schemas.android.com/apk/res/com.example.hiddenline"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

一般是:http://schemas.android.com/apk/res/工程名

這樣就可以在下面定義的佈局文件中直接使用了.

<com.example.hiddenline.HiddenLine
    android:layout_width = "match_parent"
    android:layout_height = "1dp"
    android:layout_marginTop = "20dp"
    hiddenLine:textColor = "#f84d4d"/>

其中:textColor 是在自定義view的時候,定義的屬性.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name = "hiddenline">
    <attr name = "textColor" format= "color" />
</declare-styleable >
</resources>

這就是簡單的實現了自定義命名空間.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章