我试图在第二个活动中实现工具栏。但是我遇到了麻烦。
Activity.java(AddActivity)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
public class AddActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
initToolbar();
Button close = (Button) findViewById(R.id.button2222);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
private void initToolbar(){
Toolbar toolbar = (Toolbar) findViewById(R.id.add_app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_action_navigation_close);
}
}
app_bar文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar android:id="@+id/generic_toolbar"
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"/>
活动布局文件
<?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:id="@+id/activity_add"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.daveslab.wideview.AddActivity">
<include
android:id="@+id/add_app_bar"
layout="@layout/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="129dp"
android:id="@+id/button2222" />
</RelativeLayout>
问题: - 通知栏是白色的 - 应用栏没有阴影 - 它不作为工具栏
我也试过(主工具栏布局):
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="com.daveslab.wideview.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_create_white_48px" />
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:0)
解决了,我添加了一个新的活动,工具栏的安装方式正确(仅限AndroidStudio 2.1 +)