需要帮助在Android中使用多个按钮自定义视图

需要帮助在Android中使用多个按钮自定义视图,android,view,android-canvas,android-view,android-custom-view,Android,View,Android Canvas,Android View,Android Custom View,到目前为止,我已经通过扩展ImageView类完成了自定义视图。 现在, 我必须为绘图和屏幕上的两个按钮创建自定义视图 我必须分别显示按钮和自定义视图。按钮不应覆盖自定义视图 怎么做? 需要你的帮助 我的示例代码 目前,我正在通过以下方法从主活动调用customview类: MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {

到目前为止,我已经通过扩展ImageView类完成了自定义视图。

现在,

我必须为绘图和屏幕上的两个按钮创建自定义视图

我必须分别显示按钮和自定义视图。按钮不应覆盖自定义视图

怎么做?

需要你的帮助

我的示例代码

目前,我正在通过以下方法从主活动调用customview类:

MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    CustomeView tv = (CustomeView) 
    setContentView(tv);

    }
}

您可以尝试使用XML布局,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_weight="1"
            android:text="Button 1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_weight="1"
            android:text="Button 2" />
    </LinearLayout>

    <com.androiddom.customview
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_margin="20dip"
        android:layout_weight="1" >
    </com.androiddom.customview>

</LinearLayout>
MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Initialize your CustomeView object in this way 
        // then do what you want to do with this object
        CustomeView tv = (CustomeView) findViewById(R.id.custom_view);

        Button bt = (Button)findViewById(R.id.button);
        btPrevious.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) { 

        tv.clear();

    }

}
// start new drawing
public void startNew() {
    canvas.drawColor(0, PorterDuff.Mode.CLEAR);
    invalidate();
}
另一个更新:

创建一个新方法来清除
CustomeView.java
类中的工程视图,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_weight="1"
            android:text="Button 1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_weight="1"
            android:text="Button 2" />
    </LinearLayout>

    <com.androiddom.customview
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_margin="20dip"
        android:layout_weight="1" >
    </com.androiddom.customview>

</LinearLayout>
MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Initialize your CustomeView object in this way 
        // then do what you want to do with this object
        CustomeView tv = (CustomeView) findViewById(R.id.custom_view);

        Button bt = (Button)findViewById(R.id.button);
        btPrevious.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) { 

        tv.clear();

    }

}
// start new drawing
public void startNew() {
    canvas.drawColor(0, PorterDuff.Mode.CLEAR);
    invalidate();
}
然后在
onClick()中调用此方法

@Override
public void onClick(View v) { 

    tv.startNew();

}

你可以从

中找到更多关于自定义绘图的帮助。我必须创建绘图应用程序,在触摸屏上绘制手指线。我尝试了,但只显示了自定义视图。你能告诉我从主活动中如何调用这个custome view类吗?使用XML进行排列。嘿,还有一个帮助,我想从点击按钮中清除视图。我正在向custome视图添加CLEAR方法:在onclick中调用该方法时,什么也没发生。视线不清。