gpt4 book ai didi

java - setSupportActionBar(toolbar) 即使导入 android.support.v7.widget.Toolbar 后也会出现运行时错误

转载 作者:行者123 更新时间:2023-12-01 18:10:54 25 4
gpt4 key购买 nike

这是主要 Activity ,如 您可以清楚地看到 import android.support.v7.widget.Toolbar; 已经存在。

<小时/>
package app.com.example.anandujjwal.zoom;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar( toolbar );
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
<小时/>

主要 Activity xml 文件如下。setSupportActionBar(toolbar)

处存在运行时错误<小时/>
<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"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"
/>

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
<小时/>

styles.xml 如下

<小时/>
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#006400</item>
<item name="android:windowActionBar">false</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:textColorSecondary">#FFFFFF</item>
</style>

</resources>

最佳答案

在 styles.xml 中使用它:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
</style>

使用以下代码在值中创建colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#069985</color>
<color name="colorPrimaryDark">#068573</color>
<color name="colorAccent">#fff</color>
</resources>

在onCreate中:

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar( toolbar );
}

你的toobar xml是正确的

关于java - setSupportActionBar(toolbar) 即使导入 android.support.v7.widget.Toolbar 后也会出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com