我自定义了一个MyRadioButton继承自android.widget.RadioButton,然后在loyout文件中注册如下
<com.selfDefinedThings.MyRadioButton 
        android:id="@+id/selfid"
        zlt:fff="初始值"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="sfeee"
        ></com.selfDefinedThings.MyRadioButton>
我在Activity中用findViewById方法中用R.id后面得不到我自己定义的索引,怎么办?

解决方案 »

  1.   

    你看看你的R是import那里的,是gen下面的那个R文件么,你的zlt命名空间不知道怎么写的
      

  2.   

    我的attrs.xml文件里是这样定义的,
    <resources>    <declare-styleable name="MyRadioButton">
            <attr name="fff" format="string"/>
        </declare-styleable>
        
    </resources>
    不知道zlt命名空间要改成什么,不是随便定义的吗?
      

  3.   

    忘说了,我的layout整个文件里除了第一行是这样的
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:zlt="http://schemas.android.com/apk/res/com.fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >    <com.selfDefinedThings.MyRadioButton 
            android:id="@+id/selfid"
            zlt:fff="初始值"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="sfeee"
            ></com.selfDefinedThings.MyRadioButton></LinearLayout>
      

  4.   

    你走一下这个流程:确定 这个路径 com.selfDefinedThings.MyRadioButton 是对的你的 mainfast.xml 里面的packagename 是 com.fragment 如果是,那么你的自定义view怎么没有在你的包里面呢,你把这个文件考到 com.fragment 包或其子包下,com.selfDefinedThings.MyRadioButton 这个路径相应修改。你在clean一下, 然后把 import .R 这行删了,重新引入试试, 如果正常的话 引入的应该是 com.fragment.R
      

  5.   


    是,我的packagename 是在这个下 com.fragment