Activity类型中的方法setActionBar(工具栏)不适用于参数(工具栏)

时间:2015-04-14 21:18:12

标签: android android-actionbar android-toolbar

这是工具栏布局的XML ...

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mToolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.Toolbar>

我正在使用支持库将工具栏用作我的ActionBar。我是在onCreate中做到的......

Toolbar mToolbar = (Toolbar)getLayoutInflater().inflate(R.layout.toolbar, null);
setActionBar(mToolbar);

但是,如果给我红色的波浪形,并告诉我标题中的消息。我想,LULWUT?!

2 个答案:

答案 0 :(得分:8)

您可能需要setSupportActionBar。使用Toolbar表示您应该使用AppCompatActivity,并且所有这些内容都存在于appcompat(~support)库中。

修改:如果您使用现已弃用的ActionBarActivity,则此答案仍然适用,因为ActionBarActivity inherits the setSupportActionBar method from AppCompactActivity

答案 1 :(得分:0)

2019更新

根据API 29,工具栏现在位于软件包# Termal velocity of a falling ball using DifferentialEquations using Plots g = 9.8 # Accelaration of gravity rho = 1.2 # Density of air m = 0.100 # A 100 g ball r = 0.10 # 10 cm radius Cd = 0.5 # Drag coeficient for a small spherical object y0 = 1000.0 # Initial height of the body (1000 m) v0 = 10.0 # Initial velocity of the body (10 m/s^2, going up) A = pi*r^2 # Cross-section area of the u0 = [y0, v0] # Initial Conditions tspan = (0.0,5.0) # Time span to solve for p = [g rho m Cd A] function Terminal_Velocity(du,u,p,t) (g, rho, m, Cd, A) = p du[1] = u[2] # velocity du[2] = -g - 0.5 * sign(u[2]) * (rho/m) * (u[2]^2) * Cd * A # acceleration end prob = ODEProblem(Terminal_Velocity,u0,tspan,p) sol = solve(prob) p1 = plot(sol, vars=(1), label="Displacement") p2 = plot(sol, vars=(2), label="Velocity") plot(p1, p2) 中。因此,在布局文件中:

androidx.appcompat.widget

在Java代码中:

<androidx.appcompat.widget.Toolbar 
    android:id="@+id/toolbar"/>