我正在开发一个应用程序,我尝试添加后退按钮,但工具栏不再显示在模拟器上。这是我的代码。
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".SplashTimer"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashScreenActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity android:name=".VerificationActivity">
<activity android:name=".MainActivity" />
</activity>
<activity android:name=".DashboardActivity" />
<activity
android:name=".LoginActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity android:name=".ProblemsActivity" />
<activity
android:name=".TotalActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity android:name=".FirstPayment" />
<activity android:name=".FirstPaymentInformation" />
<activity android:name=".DeclineQuestionsActivity" />
<activity android:name=".LIkelyProblemsActivity" />
<activity
android:name=".TotalAmountActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity android:name=".ThanksActivity" />
<activity
android:name=".SignUpActivity"
android:theme="@style/SignUpTheme" />
<activity android:name=".StripePayment" />
<activity
android:name=".PaymentActivity"
android:label="@string/title_activity_payment"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".LocationActivity" />
<activity android:name=".SecondPayment" />
<activity android:name=".DriverPayment"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
的style.css
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
<style name="SignUpTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:textColorHint">#bdbdbd</item>
<item name="android:windowBackground">@color/colorWhite</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
myActivity
public class DashboardActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() == null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
public void mechanicFN (View view){
Intent intent = new Intent(DashboardActivity.this, LIkelyProblemsActivity.class); // Create an Intent
startActivity(intent); // Move to Verification Activity
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.logout){
SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().clear().commit();
Intent intent = new Intent(DashboardActivity.this, HomeActivity.class); // Create an Intent
startActivity(intent); // Move to Verification Activity
Toast.makeText(getApplicationContext(), "Logout Selected", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
R.layout.activity_dashboard
<?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:orientation="vertical"
android:id="@+id/activity_dashboard"
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"
android:background="@color/colorBackground"
tools:context="com.example.system2.tranxav.DashboardActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="24dp"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/cv"
android:layout_marginRight="8dp"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/mechanic"
android:onClick="mechanicFN"
android:id="@+id/imageView2" />
<TextView
android:text="Hire a Mechanic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageView2"
android:gravity="center"
android:layout_marginBottom="16dp"
android:textColor="#000"
android:id="@+id/textView2" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:layout_height="wrap_content"
android:id="@+id/cv1"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/toyin_van"
android:id="@+id/imageView3" />
<TextView
android:text="Toyin Van"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageView3"
android:gravity="center"
android:textColor="#000"
android:layout_marginBottom="16dp"
android:id="@+id/textView3" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="24dp"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/cv3"
android:layout_marginRight="8dp"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/spareparts"
android:onClick="mechanicFN"
android:id="@+id/imageView4" />
<TextView
android:text="Spare Parts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageView4"
android:gravity="center"
android:layout_marginBottom="16dp"
android:textColor="#000"
android:id="@+id/textView4" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:layout_height="wrap_content"
android:id="@+id/cv5"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<ImageView
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/electrician"
android:id="@+id/imageView5" />
<TextView
android:text="Auto Electrician"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageView5"
android:gravity="center"
android:textColor="#000"
android:layout_marginBottom="16dp"
android:id="@+id/textView5" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
enter image description here 我浏览了代码,但我无法弄清楚问题出在哪里。我尝试删除活动上的工具栏,但它不起作用。
答案 0 :(得分:0)
将主题添加到Activity
。
<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
在Manifest
<activity
android:name=".DashBoardActivity"
android:label="@string/title_activity_payment"
android:theme="@style/AppThemeNoActionBar" />
将onCreate()
修改为。
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}