我有一个带工具栏的活动,但工具栏中没有后箭头和活动标签。我不确定为什么,因为我有另一个活动使用看似相同的代码,工具栏显示后面箭头和manifest.xml中设置的活动标签值。
<!--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类
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);
有效的活动布局。
<?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活动
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拆分为具有Coordinator布局和内容布局的活动布局,但这也没有工作并且与我的格式混乱,所以我还原了它。这是为了展示我的尝试。
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
答案 0 :(得分:3)
我发现了我做错了什么。我的活动中有两次setContentView导致工具栏无法显示。
答案 1 :(得分:1)
如果您需要使用工具栏,则应在noActionBar
中为您的活动(或您的应用程序代码)设置manifest
主题,例如:
<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>
您还需要将工具栏设置为活动的操作栏,也可以设置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);
}
}