mlz*_*lz7 26 android android-layout
我有一个toolbar.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_primary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
Run Code Online (Sandbox Code Playgroud)
我的另一个活动布局包含一个include标记:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme">
<include
layout="@layout/toolbar"
android:id="@+id/tb">
</include>
<android.support.v7.widget.RecyclerView
android:id="@+id/grade_list"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
Run Code Online (Sandbox Code Playgroud)
我在我的活动中实现了它,扩展了AppCompatActivity:
Toolbar toolbar;
toolbar = (Toolbar)findViewById(R.id.tb);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)
但是,当我运行我的应用程序时出现以下错误:
Error:(26, 29) error: incompatible types: android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar
Run Code Online (Sandbox Code Playgroud)
此外,当我在编辑器中查看我的布局时,收到以下消息:
Missing styles. Is the correct theme chosen for this layout?
Use the theme combo box above the layout to choose a different layout, or to fix the theme style references.
Failed to find style 'toolbarStyle' in current layout.
Run Code Online (Sandbox Code Playgroud)
我的styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primaryDark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="colorControlNormal">@color/color_accent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我不知道我哪里出错了.在编辑器和我的清单文件中选择了正确的主题.所有帮助一如既往地受到赞赏!
Gen*_*kin 73
您android.support.v7.widget.Toolbar在XML中使用,但在您导入的java代码中android.widget.Toolbar,这是一种不同的类型.将导入更改为android.support.v7.widget.Toolbar,它应该工作.
如果您的项目已经迁移到 AndroidX 然后导入这个
import androidx.appcompat.widget.Toolbar;
else 迁移到 AndroidX 通过
重构>迁移到 AndroidX
XML 和 Java 的完整代码示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Events.Events_Main_Activity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarlayout_id"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingtoolbar_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="8dp"
app:expandedTitleMarginStart="8dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:title="Anime Title"
app:titleEnabled="true">
<ImageView
android:id="@+id/aa_thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_sec"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="shshhs" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
package com.uafappdevelopers.example;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
//note this import
import androidx.appcompat.widget.Toolbar;
import com.uafappdevelopers.uafportal.R;
public class Events_Second_Activity extends AppCompatActivity {
//add this for the navigation back button click event
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
finish();
return super.onSupportNavigateUp();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_second);
//change id to Your id
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_sec);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
setTitle("University of Agriculture Faisalabad");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37867 次 |
| 最近记录: |