2016-08-08 104 views
-1

如何添加我的操作栏来查看(画布)类?如何将我的操作栏添加到我的视图班?

public class MainActivity extends AppCompatActivity { 


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


    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 


    //set paint color 

    setContentView(R.layout.activity_main); 
    //setContentView(new DrawView(this)); 


    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
    setSupportActionBar(toolbar); 
    //DrawView.setPaint(255, 235, 191, 47); 
} 

Here is my project.

这里是我的项目 和工具栏保存在activity_main.xml中。我想要什么

可视化表示:P 左截屏是当我使用setContentView(R.layout.activity_main);,正确的是//setContentView(new DrawView(this)); enter image description here

谢谢你们!如果它确定可以你们包括。一个解释呢?我是新来的Android编程:)

activity_main 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> 
+1

为什么不能你把画布在活动中查看与工具栏? –

+0

@ cricket_007你的意思是这样吗? http://puu.sh/qu73r/5df21ed733.png – Google0593

+0

不完全...您如何获得工具栏?你可以将你的DrawView添加到'activity_main.xml'中,你不需要从Java代码 –

回答

0

在与工具栏,按钮视图的布局编辑器,在编辑器中向下滚动到你看到“自定义视图” 。

从那里,你可以开始输入,你可以添加自定义的View类,并添加你想要的XML的任何地方。

custom view

0

你activity_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> 

然后在您的活动做

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    DrawView drawView = findViewById(R.id.drawView); 
} 
相关问题