关于R.styleable的问题

原来想直接想调用程序的东西,但是使用R.styleable的时候 eclipse不能解析了,后来发现原来被删除了此方法

 

Java代码   收藏代码
  1. public ImageAdapter(Context c) {  
  2.     mContext = c;  
  3.     TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);  
  4.     mGalleryItemBackground = a.getResourceId(  
  5.             android.R.styleable.Theme_galleryItemBackground, 0);  
  6.     a.recycle();  
  7. }  

 这种方法也就不能用了

 

然后

 

public ImageAdapter(Context c) {     mContext = c;     TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);     mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);     a.recycle(); }

 

唯一要做的就是自己去做一个styleable

res\values 

 

<?xml version="1.0" encoding="utf-8"?> <resources>     <declare-styleable name="Gallery1">         <attr name="android:galleryItemBackground" />     </declare-styleable> </resources>

以launcher为例说明自定义控件的属性:

1、  在layout里面定义控件,如:

<com.junction.launcher.DragLayer

     xmlns:android="http://schemas.android.com/apk/res/android"

     xmlns:launcher="http://schemas.android.com/apk/res/com.junction.launcher"

    android:id="@+id/drag_layer" android:layout_width="match_parent"  android:layout_height="match_parent">

  <com.android.launcher.Workspace

        android:id="@+id/workspace"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        launcher:defaultScreen="1">

 

        <include android:id="@+id/cell1" layout="@layout/workspace_screen" />

        <include android:id="@+id/cell2" layout="@layout/workspace_screen" />

        <include android:id="@+id/cell3" layout="@layout/workspace_screen" />

    </com.android.launcher.Workspace>

2、  该控件workspace下面有一个属性是launcher:defaultScreen,这属性不是ViewGroup(workspace类是继承于ViewGroup)所定义的属性,那么我们可以在\res\values里面的attrs.xml里面定义

具体的定义方法如下:

<resources>

<declare-styleable name="Workspace">

    <attr name="defaultScreen" format="integer"  />

    </declare-styleable>

</resources>

引用方法是 先申明xmlns:launcher="http://schemas.android.com/apk/res/com.junction.launcher"(R.java),这样就可以使用launcher:defaultScreen。

3、  这样我们在Workspace的类里面就可以去除在layout里面对该属性的赋值,方法如下:

Int mDefaultScreen;

 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0);

mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1);

a.recycle();

++++++++++++++++++++++++++++++

自己案例:可看一个gifPlayer的demo程序。

其他案例:http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx

 

 

转载于:https://www.cnblogs.com/yaya-Android/p/4566870.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值