【问题标题】:How put in LinearLayout class which extends GLSurfaceView?如何放入扩展 GLSurfaceView 的 LinearLayout 类?
【发布时间】:2011-06-04 16:57:09
【问题描述】:

我的问题很简单。我有扩展 GLSurfaceView 的类 TouchSurfaceView。我想在底部创建三个 TextView 和 Button 以及顶部的 TouchSurfaceView 的活动,但我不知道如何在 XML 布局中读取它。

【问题讨论】:

    标签: android xml android-linearlayout glsurfaceview


    【解决方案1】:

    您只需使用全名(包括包)引用它,就好像它是任何其他 Vew:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <your.package.TouchSurfaceView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text"
            />
    <!-- etcetera -->
    </LinearLayout>
    

    您需要一个以 AttributeSet 作为参数的构造函数(在膨胀 xml 时使用):

    public TouchSurfaceView(Context context, AttributeSet attr) {
      super(context, attr);
      // the rest of your code
    }
    

    【讨论】:

    • 谢谢,但我确实试试这个......我的包是 cz.audiorecenze 所以我尝试 &lt;cz.audiorecenze.TouchSurfaceView android:layout_width=.... 但 logcat 写道:06-05 08:50:52.025: ERROR/AndroidRuntime(543): java.lang.RuntimeException: Unable to start activity ComponentInfo{cz.audiorecenze/cz.audiorecenze.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class cz.audiorecenze.TouchSurfaceView
    • @west44 没有任何代码,很难说更多,但一个可能的原因是缺少正确的构造函数。请参阅上面的更新回复。
    • 是的,你是对的。问题出在具有两个参数(上下文,AttributeSet)的构造函数中,而我的构造函数仅用于(上下文)。谢谢你的帮助;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多