Android 如何将操作栏添加到视图类?

Android 如何将操作栏添加到视图类?,android,android-layout,android-6.0-marshmallow,Android,Android Layout,Android 6.0 Marshmallow,如何将操作栏添加到视图(画布)类 这是我的项目 工具栏存储在activity_main.xml中 我想要的东西的视觉表现:p 左屏幕截图是我使用setContentView(R.layout.activity_main)时的屏幕截图,右边的是//setContentView(新的DrawView(this)); 谢谢大家!如果可以的话,你们能给我一个解释吗?我是android编程新手:) 活动\u主XML <?xml version="1.0" encoding="utf-8"?&g

如何将操作栏添加到视图(画布)类

这是我的项目 工具栏存储在activity_main.xml中

我想要的东西的视觉表现:p 左屏幕截图是我使用
setContentView(R.layout.activity_main)时的屏幕截图,右边的是
//setContentView(新的DrawView(this));

谢谢大家!如果可以的话,你们能给我一个解释吗?我是android编程新手:)

活动\u主XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.nikol.touchpaint.MainActivity">
<!--    <view
        android:id="@+id/viewid"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />-->

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <TextView
        android:id="@+id/testText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="151dp"
        android:text="Hello World!" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button2"
        android:layout_toStartOf="@+id/button2"
        android:text="New Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="29dp"
        android:layout_marginEnd="30dp"
        android:text="New Button" />

</RelativeLayout>

在布局编辑器中,使用工具栏和按钮,在编辑器中向下滚动到“自定义视图”位置

从那里,您可以开始键入,并且可以添加自定义的
视图
类,并在XML中添加所需的任何位置


您的活动\u main.xml应该如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/contentView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.nikol.touchpaint.DrawView
        android:id="@+id/drawView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

</RelativeLayout>

为什么不能用工具栏将画布视图放在活动中?@cricket\u 007你的意思是这样的吗?不完全是。。。你怎么把工具栏放在那里?您可以将DrawView添加到
活动\u main.xml
,不需要从Java中进行设置code@cricket_007哦,我确实这样做了,但问题是当我这样做时,
//setContentView(newdrawview(this))setContentView(R.layout.activity_main)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/contentView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.nikol.touchpaint.DrawView
        android:id="@+id/drawView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

</RelativeLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DrawView drawView = findViewById(R.id.drawView);
}