首先看看函数的定义
<span style="font-size:18px;"><span style="font-size:18px;">public final TypedArray obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)</span></span>
TypedArray是一个用于存放恢复obtainStyledAttributes(AttributeSet, int[], int, int)或 obtainAttributes(AttributeSet, int[])
值的一个数组容器,当操作完成以后,一定要调用recycle()方法。用于检索的索引值在这个结构对应的位置给obtainStyledAttributes属性。
使用这个类的时候,先要在valuse文件夹下创建:atts.xml文件:
<span style="font-size:18px;"><declare-styleable name="EventsListFragment">
<attr name="header" format="reference|dimension" />
</declare-styleable></span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<com.livestream.android.widgets.KeyboardAwareLinearLayout android:orientation="vertical" android:id="@id/fr_discover_root_layout" android:background="#ffdde2e8" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<fragment android:name="com.livestream.android.fragment.HeaderFragment" android:id="@id/fr_discover_header_fragment" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
<fragment android:name="com.livestream.android.fragment.EventsFragment" android:id="@id/fr_discover_events_list_fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" app:header="@layout/tr_fr_search_listview_header"/>
<FrameLayout android:id="@id/fr_search_listview_layout" android:background="#ff303030" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="@id/fr_search_listview" android:background="@color/transparent" android:visibility="visible" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="#ff262626" android:dividerHeight="1.0dip" />
</FrameLayout>
</FrameLayout>
</com.livestream.android.widgets.KeyboardAwareLinearLayout></span>
上面布局中的
<span style="font-size:18px;"><fragment android:name="com.livestream.android.fragment.EventsFragment" android:id="@id/fr_discover_events_list_fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" app:header="@layout/tr_fr_search_listview_header"/></span>
使用了app:header来定义引入另外的布局到这个布局中,其调用方式为如下两步:
第一步我们在onCreateView中inflate第一个xml布局文件
<span style="font-size:18px;">@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fr_events_grid, null);
emptyViewMessage=(TextView)view.findViewById(R.id.fr_events_list_empty_view_message);
emptyViewTitle=(TextView)view.findViewById(R.id.fr_events_list_empty_view_title);
emptyViewImage=(ImageView)view.findViewById(R.id.fr_events_list_empty_view_image_view);
this.adapter = createAdapter();
listView=(ListView)view.findViewById(R.id.ac_suggested_accounts_listview);
if (this.headerResId != 0)
{
View localView = LayoutInflater.from(getActivity()).inflate(this.headerResId, null);
this.listView.addHeaderView(localView);
}
this.listView.setAdapter(this.adapter);
//showContentView();
//showEmptyView();
this.gotInitialEventsSet=true;
updateUiIfNeeded();
return view;
//return super.onCreateView(inflater, container, savedInstanceState);
}
</span>
第二步,我们利用onInflate来引入我们自定义的视图部分
@Override
public void onInflate(Activity activity, AttributeSet attrs,
Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onInflate(activity, attrs, savedInstanceState);
TypedArray localTypedArray = activity.obtainStyledAttributes(attrs,R.styleable.EventsListFragment );
this.headerResId = localTypedArray.getResourceId(0, 0);
localTypedArray.recycle();
}
利用上述的方式我们就可以在视图中正确使用我们自定义的视图了