android菜瓜笔记之TypedArray和obtainStyledAttributes使用场景

http://blog.csdn.net/hack8/article/details/26510015

首先看看函数的定义

  1. <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文件:

  1. <span style="font-size:18px;"><declare-styleable name="EventsListFragment">  
  2.         <attr name="header" format="reference|dimension" />           
  3. </declare-styleable></span>  


在某个布局文件中我们会使用到该自定义的样式
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <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"  
  3.   xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  
  4.     <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" />  
  5.     <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent">  
  6.         <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"/>  
  7.         <FrameLayout android:id="@id/fr_search_listview_layout" android:background="#ff303030" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  8.             <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" />  
  9.         </FrameLayout>  
  10.     </FrameLayout>  
  11. </com.livestream.android.widgets.KeyboardAwareLinearLayout></span>  


上面布局中的
  1. <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布局文件

  1. <span style="font-size:18px;">@Override  
  2.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  3.             Bundle savedInstanceState)  
  4.     {  
  5.         // TODO Auto-generated method stub  
  6.           
  7.         View view = inflater.inflate(R.layout.fr_events_grid, null);          
  8.         emptyViewMessage=(TextView)view.findViewById(R.id.fr_events_list_empty_view_message);  
  9.         emptyViewTitle=(TextView)view.findViewById(R.id.fr_events_list_empty_view_title);  
  10.         emptyViewImage=(ImageView)view.findViewById(R.id.fr_events_list_empty_view_image_view);  
  11.         this.adapter = createAdapter();  
  12.           
  13.         listView=(ListView)view.findViewById(R.id.ac_suggested_accounts_listview);  
  14.         if (this.headerResId != 0)  
  15.         {  
  16.           View localView = LayoutInflater.from(getActivity()).inflate(this.headerResId, null);  
  17.           this.listView.addHeaderView(localView);  
  18.         }  
  19.           
  20.           
  21.           
  22.         this.listView.setAdapter(this.adapter);  
  23.           
  24.         //showContentView();  
  25.         //showEmptyView();  
  26.         this.gotInitialEventsSet=true;  
  27.         updateUiIfNeeded();  
  28.           
  29.         return view;  
  30.           
  31.         //return super.onCreateView(inflater, container, savedInstanceState);  
  32.     }  
  33.   
  34. </span>  

第二步,我们利用onInflate来引入我们自定义的视图部分

  1. @Override  
  2.     public void onInflate(Activity activity, AttributeSet attrs,  
  3.             Bundle savedInstanceState)  
  4.     {  
  5.         // TODO Auto-generated method stub  
  6.         super.onInflate(activity, attrs, savedInstanceState);  
  7.         TypedArray localTypedArray = activity.obtainStyledAttributes(attrs,R.styleable.EventsListFragment );  
  8.         this.headerResId = localTypedArray.getResourceId(00);  
  9.         localTypedArray.recycle();  
  10.           
  11.     }  

利用上述的方式我们就可以在视图中正确使用我们自定义的视图了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值