为什么这个 android 工具栏是空的?
Why is this android toolbar empty?
我有一个带工具栏的 activity,但工具栏中既没有显示后退箭头,也没有显示 activity 的标签。我不确定为什么,因为我有另一个 activity 使用看似相同的代码并且工具栏显示后退箭头和 manifest.xml 中设置的 activity 标签值。
<!--This is the activity where the toolbar doesn't work-->
<activity
android:name=".CreateReviewActivity"
android:label="@string/title_activity_create_review"
android:parentActivityName=".VenuesToReviewActivity"
android:theme="@style/Theme.Phield">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.tremulant.phield.MainActivity"/>
</activity>
<!--This activity's toolbar works-->
<activity
android:name=".ReviewDetailsActivity"
android:label="@string/title_activity_review_details"
android:parentActivityName=".VenuesToReviewActivity"
android:theme="@style/Theme.Phield">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.tremulant.phield.VenuesToReviewActivity"/>
</activity>
工具栏布局。
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flickr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
flickr:theme="@style/ActionBarThemeOverlay"
flickr:titleTextAppearance="@style/ActionBar.TitleText">
</android.support.v7.widget.Toolbar>
适合衡量的字符串
<string name="title_activity_create_review">Create Review</string>
<string name="title_activity_review_details">Review Details</string>
布局无效
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:phield="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:background="@color/phieldBackgroundColor"
tools:context=".CreateReviewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Phield.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
phield:popupTheme="@style/Theme.Phield.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</LinearLayout>
java activity class that isn't working
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:phield="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:background="@color/phieldBackgroundColor"
tools:context=".CreateReviewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Phield.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
phield:popupTheme="@style/Theme.Phield.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</LinearLayout>
Java class 无效
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.tremulant.phield.data.Review;
import com.tremulant.phield.data.User;
import com.tremulant.phield.data.Venue;
import com.tremulant.phield.data.helpers.StringWithTag;
import com.tremulant.phield.webservices.WebServiceTask;
import com.tremulant.phield.webservices.WebServiceUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.tremulant.phield.Constants.DEFAULT_REVIEW_TEXT;
public class CreateReviewActivity extends BaseActivity implements RatingBar.OnRatingBarChangeListener, View.OnClickListener, AdapterView.OnItemSelectedListener{
private User mUser;
private Venue mVenue;
private Review mReview;
private Button btnSubmit;
private EditText mEditText;
private TextView mUserTextCounter;
private boolean mbEditTextFocusedOnce;
private CreateReviewServiceTask mCreateReviewServiceTask;
private static final String LOG_TAG = "CreateReviewActivity";
//private RatingBar mRatingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkUserTokensSet();
setContentView(R.layout.activity_create_review);
//activateToolbarWithHomeEnabled();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
activity 有效的布局。
<?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.tremulant.phield.ReviewDetailsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Phield.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/Theme.Phield.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<include layout="@layout/content_review_details"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
有效且似乎相关的内容部分
<RelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.tremulant.phield.ReviewDetailsActivity"
tools:showIn="@layout/activity_review_details">
Java activity 有效
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.tremulant.phield.data.AggReview;
import com.tremulant.phield.data.User;
import com.tremulant.phield.webservices.WebServiceTask;
import com.tremulant.phield.webservices.WebServiceUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ReviewDetailsActivity extends BaseActivity implements View.OnClickListener, OnMapReadyCallback{
private User mUser;
private AggReview mAggReview;
private ReviewTextAdapter mReviewTextAdapter;
private GoogleMap mMap;
private LatLng mLatLng;
private RetrieveReviewTextTask mRetrieveReviewTextTask;
private static final String LOG_TAG = "ReviewDetailsActivity";
private List<ReviewText> mReviewTexts = new ArrayList<ReviewText>();
@Override
public void onClick(View v) {
Intent mapIntent = new Intent(ReviewDetailsActivity.this, VenueMap.class);
mapIntent.putExtra(VENUE_TRANSFER,mAggReview);
startActivity(mapIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkUserTokensSet();
setContentView(R.layout.activity_review_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="Theme.Base" parent="AppTheme">
<item name="colorPrimary">@color/phieldPrimaryColor</item>
<item name="colorPrimaryDark">@color/phieldPrimaryDarkColor</item>
<item name="colorAccent">@color/phieldAccentColor</item>
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/phieldBackgroundColor</item>
<item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
<item name="android:spinnerItemStyle">@style/mySpinnerItemStyle</item>
</style>
<style name="Theme.Phield" parent="Theme.Base"/>
<style name="ActionBarThemeOverlay" parent="">
<item name="android:textColorPrimary">@color/phieldPrimaryTextColor</item>
<item name="colorControlHighlight">@color/phieldAccentColor</item>
<item name="android:actionMenuTextColor">@color/phieldBackgroundColor</item>
<item name="android:textColorSecondary">@color/phieldBackgroundColor</item>
<item name="android:background">@color/phieldPrimaryColor</item>
</style>
<style name="ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/phieldBackgroundColor</item>
<item name="android:textSize">18sp</item>
</style>
<style name="Theme.Phield.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="Theme.Phield.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<style name="FullscreenTheme" parent="Theme.Phield">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
<item name="colorPrimary">@color/md_black_1000</item>
<item name="colorPrimaryDark">@color/md_black_1000</item>
</style>
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/black_overlay</item>
</style>
<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">@color/phieldPrimaryColor</item>
</style>
<style name="mySpinnerSelectedItemStyle"
parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">@color/phieldPrimaryColor</item>
</style>
</resources>
我也曾尝试将我的 activity_create_reviews 拆分为具有协调器布局和内容布局的 activity 布局,但这也没有用,并且弄乱了我的格式,所以我恢复了它。这里是为了展示我的尝试。
activity_create_reviews.xml
<?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.tremulant.phield.CreateReviewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Phield.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/Theme.Phield.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<include layout="@layout/content_create_review"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
content_create_reviews.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.tremulant.phield.CreateReviewActivity"
tools:showIn="@layout/activity_create_review">
<!-- <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/phieldBackgroundColor"
>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp"
>
<TextView
android:id="@+id/venue_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/phieldPrimaryTextColor"
android:textSize="14sp"/>
<!--etc more layouts and things unrelate to toolbar
如果您需要使用工具栏,您应该在 manifest
中为您的 activity(或您的应用程序标签)设置 noActionBar
主题,例如:
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
您还需要将工具栏设置为 activity 的操作栏,同时覆盖 onCreateOptionMenu
,如下所示:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar; // Declaring the Toolbar Object
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call
}
@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 中设置了两次 setContentView,导致工具栏无法显示。
我有一个带工具栏的 activity,但工具栏中既没有显示后退箭头,也没有显示 activity 的标签。我不确定为什么,因为我有另一个 activity 使用看似相同的代码并且工具栏显示后退箭头和 manifest.xml 中设置的 activity 标签值。
<!--This is the activity where the toolbar doesn't work-->
<activity
android:name=".CreateReviewActivity"
android:label="@string/title_activity_create_review"
android:parentActivityName=".VenuesToReviewActivity"
android:theme="@style/Theme.Phield">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.tremulant.phield.MainActivity"/>
</activity>
<!--This activity's toolbar works-->
<activity
android:name=".ReviewDetailsActivity"
android:label="@string/title_activity_review_details"
android:parentActivityName=".VenuesToReviewActivity"
android:theme="@style/Theme.Phield">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.tremulant.phield.VenuesToReviewActivity"/>
</activity>
工具栏布局。
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flickr="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
flickr:theme="@style/ActionBarThemeOverlay"
flickr:titleTextAppearance="@style/ActionBar.TitleText">
</android.support.v7.widget.Toolbar>
适合衡量的字符串
<string name="title_activity_create_review">Create Review</string>
<string name="title_activity_review_details">Review Details</string>
布局无效
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:phield="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:background="@color/phieldBackgroundColor"
tools:context=".CreateReviewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Phield.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
phield:popupTheme="@style/Theme.Phield.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</LinearLayout>
java activity class that isn't working
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:phield="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:background="@color/phieldBackgroundColor"
tools:context=".CreateReviewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Phield.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
phield:popupTheme="@style/Theme.Phield.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</LinearLayout>
Java class 无效
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.tremulant.phield.data.Review;
import com.tremulant.phield.data.User;
import com.tremulant.phield.data.Venue;
import com.tremulant.phield.data.helpers.StringWithTag;
import com.tremulant.phield.webservices.WebServiceTask;
import com.tremulant.phield.webservices.WebServiceUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.tremulant.phield.Constants.DEFAULT_REVIEW_TEXT;
public class CreateReviewActivity extends BaseActivity implements RatingBar.OnRatingBarChangeListener, View.OnClickListener, AdapterView.OnItemSelectedListener{
private User mUser;
private Venue mVenue;
private Review mReview;
private Button btnSubmit;
private EditText mEditText;
private TextView mUserTextCounter;
private boolean mbEditTextFocusedOnce;
private CreateReviewServiceTask mCreateReviewServiceTask;
private static final String LOG_TAG = "CreateReviewActivity";
//private RatingBar mRatingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkUserTokensSet();
setContentView(R.layout.activity_create_review);
//activateToolbarWithHomeEnabled();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
activity 有效的布局。
<?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.tremulant.phield.ReviewDetailsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Phield.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/Theme.Phield.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<include layout="@layout/content_review_details"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
有效且似乎相关的内容部分
<RelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.tremulant.phield.ReviewDetailsActivity"
tools:showIn="@layout/activity_review_details">
Java activity 有效
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.tremulant.phield.data.AggReview;
import com.tremulant.phield.data.User;
import com.tremulant.phield.webservices.WebServiceTask;
import com.tremulant.phield.webservices.WebServiceUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ReviewDetailsActivity extends BaseActivity implements View.OnClickListener, OnMapReadyCallback{
private User mUser;
private AggReview mAggReview;
private ReviewTextAdapter mReviewTextAdapter;
private GoogleMap mMap;
private LatLng mLatLng;
private RetrieveReviewTextTask mRetrieveReviewTextTask;
private static final String LOG_TAG = "ReviewDetailsActivity";
private List<ReviewText> mReviewTexts = new ArrayList<ReviewText>();
@Override
public void onClick(View v) {
Intent mapIntent = new Intent(ReviewDetailsActivity.this, VenueMap.class);
mapIntent.putExtra(VENUE_TRANSFER,mAggReview);
startActivity(mapIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkUserTokensSet();
setContentView(R.layout.activity_review_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="Theme.Base" parent="AppTheme">
<item name="colorPrimary">@color/phieldPrimaryColor</item>
<item name="colorPrimaryDark">@color/phieldPrimaryDarkColor</item>
<item name="colorAccent">@color/phieldAccentColor</item>
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/phieldBackgroundColor</item>
<item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
<item name="android:spinnerItemStyle">@style/mySpinnerItemStyle</item>
</style>
<style name="Theme.Phield" parent="Theme.Base"/>
<style name="ActionBarThemeOverlay" parent="">
<item name="android:textColorPrimary">@color/phieldPrimaryTextColor</item>
<item name="colorControlHighlight">@color/phieldAccentColor</item>
<item name="android:actionMenuTextColor">@color/phieldBackgroundColor</item>
<item name="android:textColorSecondary">@color/phieldBackgroundColor</item>
<item name="android:background">@color/phieldPrimaryColor</item>
</style>
<style name="ActionBar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">@color/phieldBackgroundColor</item>
<item name="android:textSize">18sp</item>
</style>
<style name="Theme.Phield.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="Theme.Phield.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<style name="FullscreenTheme" parent="Theme.Phield">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
<item name="colorPrimary">@color/md_black_1000</item>
<item name="colorPrimaryDark">@color/md_black_1000</item>
</style>
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/black_overlay</item>
</style>
<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">@color/phieldPrimaryColor</item>
</style>
<style name="mySpinnerSelectedItemStyle"
parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">@color/phieldPrimaryColor</item>
</style>
</resources>
我也曾尝试将我的 activity_create_reviews 拆分为具有协调器布局和内容布局的 activity 布局,但这也没有用,并且弄乱了我的格式,所以我恢复了它。这里是为了展示我的尝试。
activity_create_reviews.xml
<?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.tremulant.phield.CreateReviewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Phield.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/Theme.Phield.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
<include layout="@layout/content_create_review"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
content_create_reviews.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.tremulant.phield.CreateReviewActivity"
tools:showIn="@layout/activity_create_review">
<!-- <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/phieldBackgroundColor"
>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp"
>
<TextView
android:id="@+id/venue_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/phieldPrimaryTextColor"
android:textSize="14sp"/>
<!--etc more layouts and things unrelate to toolbar
如果您需要使用工具栏,您应该在 manifest
中为您的 activity(或您的应用程序标签)设置 noActionBar
主题,例如:
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
您还需要将工具栏设置为 activity 的操作栏,同时覆盖 onCreateOptionMenu
,如下所示:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar; // Declaring the Toolbar Object
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call
}
@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 中设置了两次 setContentView,导致工具栏无法显示。