首先,我在values下定义了一个atrrs.xml文件如下:<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="BookshelfView"><!-- 控件名称-->
      <attr name="total_pagenum" format="string"></attr><!-- 属性名称,类型-->
      <attr name="current_pagenum" format="string"></attr><!-- 属性名称,类型-->
   </declare-styleable>
</resources>然后,我自定义了一个名为:public class BookshelfView extends ViewFlipper implements OnGestureListener的一个自定义View,主要涉及到得属性获取代码有:
       try {
     /**
      * 将自定义的属性和XML绑定
      */
      //获取attrs.xml文件中的自定义组件
      TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BookshelfView);
      //开始绑定
               Log.e(TAG, "----------------------"+ta.getString(R.styleable.BookshelfView_total_pagenum));       //回收以免内存泄露
      ta.recycle();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
最后就是bookview.xml的界面文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  android:id="@+id/bookview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="@drawable/book_background"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:fsms="http://schemas.android.com/apk/res/com.dooland.www">
<com.dooland.www.view.BookView
  android:id="@+id/mybook"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerVertical="true"
  android:layout_centerHorizontal="true"
  fsms:total_pagenum="5" />
</RelativeLayout>
但是得到的打印结果却是:01-04 02:24:58.140: ERROR/BookshelfView(1191): ----------------------null