【问题标题】:How do i add my action bar to my view class?如何将我的操作栏添加到我的视图类?
【发布时间】:2016-08-08 16:39:08
【问题描述】:

如何将我的操作栏添加到 view(Canvas) 类?

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);
}

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

我想要的视觉表示:P 左边的屏幕截图是我使用setContentView(R.layout.activity_main);,右边的屏幕截图是//setContentView(new DrawView(this));

谢谢各位!如果可以的话,你们可以吗?还有解释吗?我是 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>

【问题讨论】:

  • 为什么不能用 Toolbar 把 Canvas 视图放到一个 Activity 中?
  • @cricket_007 你是这个意思吗? puu.sh/qu73r/5df21ed733.png
  • 不完全是...您如何获得工具栏?您可以将 DrawView 添加到 activity_main.xml 中,您不需要从 Java 代码中设置它
  • @cricket_007 哦,我做到了,但问题是当我这样做时//setContentView(new DrawView(this)); 事情消失了。
  • 我不明白你的意思...如果你想看任何东西,你需要一个内容视图。所以你最好的选择是使用setContentView(R.layout.activity_main);

标签: android android-layout android-6.0-marshmallow


【解决方案1】:

在带有工具栏和按钮的视图的布局编辑器中,在编辑器中向下滚动到您看到“自定义视图”的位置。

从那里,您可以开始输入,您可以添加您的自定义 View 类并添加到该 XML 中您想要的任何位置。

【讨论】:

    【解决方案2】:

    您的 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多