【问题标题】:Android View, creating custom componentAndroid View,创建自定义组件
【发布时间】:2013-01-29 16:12:28
【问题描述】:

首先,我知道有很多类似的问题,但我可以找到一个完整的答案来做我想做的事。这是关于创建自定义组件。

问题是,我想为我的 UI 创建一个自定义动画组件。此外,客户希望轻松更改组件的图像,因此我决定使用在代码中动画的 xml 布局。但是我好像搞不定……

我第一次尝试的例子是一个指示二氧化碳水平的针规。

有 2 个图像:仪表和针,所以我用我的两个 ImageView 创建了一个仪表布局。

<?xml version="1.0" encoding="utf-8"?>
<com.erdf.components 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<ImageView
    android:id="@+id/gauge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/jauge" />

<ImageView
    android:id="@+id/needle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/jauge_needle" />

然后我创建了关联的类:

public class components extends RelativeLayout {

    private ImageView mNeedle;

    private int mValue = 0;

    public components(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mNeedle = (ImageView)findViewById(R.id.needle);
    }


    public void SetGaugeValue(int value) {
        if(value > 180) {
            value = 180;
        }
        else if(value < -180)
        {
            value = -180;
        }

        RotateAnimation needleDeflection = new RotateAnimation(mValue, value, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        needleDeflection.setDuration(100*(Math.abs(mValue-value))); 
        needleDeflection.setFillAfter(true);

        mNeedle.startAnimation(needleDeflection);

    }
}

最后我尝试将此组件包含到我的 ActivityView 中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <include
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        layout="@layout/gauge_layout" />

</RelativeLayout>

这是活动类的代码:

public class GaugeActivity extends Activity {

    private components test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_layout);
    }
}

所以我的第一个问题是:为什么我在执行应用程序时做错了得到这个异常:

    01-29 16:04:28.886: E/AndroidRuntime(1570): FATAL EXCEPTION: main
01-29 16:04:28.886: E/AndroidRuntime(1570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gauge/com.example.gauge.GaugeActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.erdf.components
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.os.Looper.loop(Looper.java:137)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at dalvik.system.NativeStart.main(Native Method)
01-29 16:04:28.886: E/AndroidRuntime(1570): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.erdf.components
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.createView(LayoutInflater.java:596)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:807)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:736)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.Activity.setContentView(Activity.java:1881)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at com.example.gauge.GaugeActivity.onCreate(GaugeActivity.java:24)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.Activity.performCreate(Activity.java:5104)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-29 16:04:28.886: E/AndroidRuntime(1570):     ... 11 more
01-29 16:04:28.886: E/AndroidRuntime(1570): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
01-29 16:04:28.886: E/AndroidRuntime(1570):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at java.lang.Class.getConstructor(Class.java:431)
01-29 16:04:28.886: E/AndroidRuntime(1570):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
01-29 16:04:28.886: E/AndroidRuntime(1570):     ... 23 more

第二个是关于如何使用我的组件。如何在我的成员变量 test 中转换包含的布局,它是与同一包含布局关联的类的实例?然后我可以简单地使用 setGaugeValue ( ) 函数来为针设置动画。

我希望我已经足够清楚了。

【问题讨论】:

  • 您的堆栈跟踪不完整。请发一个完整的。无论如何,恕我直言标准 Android 视图不适合您想要的。我建议你检查this
  • 我不太确定,但我怀疑在使用 &lt;include&gt; 时可以使用任何额外的属性。从该节点中删除任何额外的属性并尝试。

标签: android custom-component inflate xml-layout


【解决方案1】:

“组件”不需要使用自定义的RelativeLayout,它可以只是一个普通的RelativeLayout。

在任何情况下,您都会创建对该特定 RelativeLayout 的引用

(components) comp = (components) findViewById(R.id.myComponent)
  //note that you don't have id set up yet in your custom view xml tag

那么你可以使用

comp.SetGaugeValue(5);

注意类名“components”应该以大写字母开头——java实践。

【讨论】:

  • 我不确定我是否理解你所说的正常 RelativeLayout 的意思。
  • 您是否也使用 关闭了自定义视图?您的组件 xml 不需要 xmlns:android... 已经在其父级中。普通 realtiveLayout 将不是扩展它的自定义类。
  • 但是组件类不能从 View 扩展,说 eclipse,因为它不能被转换为 ViewGroup。那么组件类应该从什么扩展?也许你有一个我可以看的例子?我从@M-WaJeEh 阅读了这篇文章,但这并不是我真正想要的......
  • 根本不创建组件类,只需使用 mNeedle = (ImageView)findViewById(R.id.needle) 并将动画应用于它。这就是为什么我说你可以只使用RelativeLayout。但是,如果您真的想要自定义视图,请尝试我的答案中的代码。
猜你喜欢
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 2011-03-06
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多