Android 自定义视图问题

Android 自定义视图问题,android,android-xml,android-custom-view,Android,Android Xml,Android Custom View,我是Android开发新手,我对自定义视图和使用xml进行视图自定义有疑问 因此,我在我的代码中使用扩展视图类定义了一个视图,即 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Display display = getWindowManager().getDefaultDisplay(); requestWindowFeatur

我是Android开发新手,我对自定义视图和使用xml进行视图自定义有疑问

因此,我在我的代码中使用扩展视图类定义了一个视图,即

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Display display = getWindowManager().getDefaultDisplay();
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);        
    draw = new DrawView(this, display.getWidth(), display.getHeight(), vibrator);
    setContentView(draw);//custom view called DrawView
}   
在DrawView类中,我使用画布执行操作

我的问题是,

  • 我可以将XML布局与我定义的视图结合使用吗

  • 我需要在这个自定义视图中添加几个按钮,在这个场景中如何实现这一点


  • 谢谢。

    您可以在布局中嵌入自定义视图。以下是一个例子:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="horizontal" android:background="#FFFFFF">
        <ListView android:id="@+id/channelsLogos" android:scrollbars="none"
            android:layout_height="fill_parent" android:layout_weight=".20"
            android:layout_width="100dip">
        </ListView>
        <test.poc.CustomScrollView
            android:id="@+id/scrollViewVertical" android:scrollbars="none"
            android:layout_weight=".80" android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        </test.poc.CustomScrollView>
    
    </LinearLayout>
    
    
    
    CustomScrollView是package test.poc中的自定义组件

    执行此操作时,请确保使用正确的构造函数