如何在Android中创建AttributeSet?

时间:2014-05-11 11:46:53

标签: android xml xmlpullparser android-attributes

我正在尝试在Android中编写一些代码,以便从attrs.xml文件中设置AttributeSet中的参数。但是我收到了“未找到资源”错误。

Java代码

MainActivity.java

package com.example.mycompoundbutton;

import org.xmlpull.v1.XmlPullParser;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Xml;
import android.app.Activity;
import android.content.res.Resources;


public class MainActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Resources res = this.getResources();

    XmlPullParser parser = res.getXml(R.attr.xyz);

    AttributeSet attrs = Xml.asAttributeSet(parser);

    MyCompound my = new MyCompound(this,attrs);

    my.MyTestFun(300,500);

}
}

MyCompound.java

package com.example.mycompoundbutton;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.CompoundButton;

public class MyCompound extends CompoundButton
{

public MyCompound(Context context, AttributeSet attrs) 
{

        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyCustomView, R.attr.xyz, 0);

        a.recycle();

}

public void MyTestFun(int x,int y)
{

     // Some Code to Execute


}}

attrs.xml

<resources>
<declare-styleable name="MyCustomView">

    <attr name="abc" format="integer"/>            
    <attr name="pqr" format="integer" />

</declare-styleable>

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

</resources>

错误:

05-11 07:29:27.345: E/AndroidRuntime(1919): FATAL EXCEPTION: main
05-11 07:29:27.345: E/AndroidRuntime(1919): java.lang.RuntimeException: Unable to start activity         ComponentInfo{com.example.mycompoundbutton/com.example.mycompoundbutton.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f010000
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.os.Looper.loop(Looper.java:137)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at java.lang.reflect.Method.invoke(Method.java:525)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at dalvik.system.NativeStart.main(Native Method)
05-11 07:29:27.345: E/AndroidRuntime(1919): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f010000
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.content.res.Resources.getValue(Resources.java:1118)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2304)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.content.res.Resources.getXml(Resources.java:983)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at com.example.mycompoundbutton.MainActivity.onCreate(MainActivity.java:24)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.app.Activity.performCreate(Activity.java:5133)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-11 07:29:27.345: E/AndroidRuntime(1919):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-11 07:29:27.345: E/AndroidRuntime(1919):     ... 11 more

所以,这里是以上所有代码和错误描述。我想从MyCompound致电MainActivity班级。我知道如何使用XML文件,例如

<com.example.mycompoundbutton.MyCompound
     ... attributes here ...
> 
</com.example.mycompoundbutton.MyCompound>

上述结构将帮助我设计静态自定义布局,但它不会帮助我设计动态布局。

那么,如何使用MyCompoundMainActivity拨打AttributeSet课程?

3 个答案:

答案 0 :(得分:1)

重新阅读后,您的一个问题在于xyz属性是在attrs.xml中的CustomViewStyleable之外声明的。


不是直接调用自定义视图的构造函数,而是让系统通过从xml中扩展自定义视图来处理它。

创建一个xml布局文件,其中仅包含您的自定义视图,例如view_my_compound.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.mycompoundbutton.MyCompound
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:xyz="2" />

然后在你的活动中,你只需要夸大它:

// the view which will contain your MyCompoundButton
View container = findViewById(R.id.XXXXXXXX);
LayoutInflater inflater = getLayoutInflater();
MyCompound button = (MyCompound) inflater.inflate(R.layout.view_my_compound, container, true);

这样您就不需要考虑视图类中的属性集了; Android将为您调用MyCompound(Context, AttributeSet)构造函数,并在XML布局文件中设置值。

如果您需要以编程方式设置XYZ参数,请在MyCompound中公开一个公共方法,以便您进行设置。

答案 1 :(得分:0)

我建议在<com.example.mycompoundbutton.MyCompound>中使用activity_main.xml标记,而不是尝试在Java代码中创建实例。这样,当您致电setContentView()而不自行玩充气机时,Android会让您的观看次数膨胀。您可以将所有标准属性应用于此标记,包括android:id。然后,您可以findViewById()来获取MyCompound视图的引用,就像使用任何标准视图一样。

答案 2 :(得分:-1)

盖伊,我认为你的问题是听到的

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyCustomView, R.attr.xyz, 0);

“R.attr.xyz”未找到。 您可以看到此click Hear