v7 widget toolbar not showing, why?

2019-09-05 13:41发布

I am so close. I have the android studio project set up correctly with libs, the project compiles, and the emulators and device run the app. I am trying to get rid of the bottom button popup menu of older devices and replace with v7 appcompat toolbar. The old action bar is gone but the menu inflate still inflates to the old popup menu. I start off using a splashscreen then the app main shows the database view activity. This means I have to dynamically load the toolbar. My thinking now is the only place I have left to work out is the styles/themes or the order of setcontent(this) to display the main app/database view screen. Also I dont have the correct emulator for the HTC evo device so the device buttons dont show. I believe this is why alot of developers post that the toolbar doesnt show. When I install apk on real device i can the use the menu button. This is how I am able to see that the menu is not in the toolbar. The emulator shows no buttons plus no toolbar so a developer on an emulator of newer devices would never see this. I hope this enough to go on for any one to catch my pitfall here. Thanks in advance. Here are codes snippets:

styles.xml:

<resources>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<!--<style name="AppTheme" parent="AppBaseTheme">-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">true</item>
    <item name="android:textColor">#000000</item>
</style>
</resources>

Toolbarmenu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu
        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" tools:context=".MainActivity">
    <item
        android:id="@+id/add"
        android:icon="@android:drawable/ic_menu_add"
        app:showAsAction="ifRoom"
        android:title="@string/app_name"/>
    <item
        android:id="@+id/reset"
        android:icon="@android:drawable/ic_menu_revert"
        app:showAsAction="ifRoom"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/about"
        android:icon="@android:drawable/ic_dialog_info"
        app:showAsAction="ifRoom"
        android:title="@string/hello_world">
    </item>
    <item
        android:id="@+id/exit"
        app:showAsAction="ifRoom"
        android:title="@string/Menu">
    </item>
</menu>

Manifest.xlm:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.loadrunner"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.loadrunner.SplashScreenActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.loadrunner.MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

lrtoolbar.xml

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="#2196f3"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        android:minHeight="200dp">
    </android.support.v7.widget.Toolbar>

</LinearLayout>

Layout_main.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context=".SplashScreenActivity"
    android:visibility="visible"
    android:background="@color/black">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewSplashLogo"
        android:src="@drawable/shoppingcartloading"
        android:visibility="visible"
        android:contentDescription="@string/app_name"
        android:scaleType="fitCenter"
        android:cropToPadding="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/black" />
  </RelativeLayout>

And finally the code:

import android.app.ActionBar;
import android.app.Activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import com.Table.TableMainLayout;


public class MainActivity extends AppCompatActivity {

    final String TAG = "MainActivity.java";
    public static DatabaseHandler db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Log.d("SplashMainActivityonCre", "made it");
        db = new DatabaseHandler(this);
        db.getWritableDatabase();
        db.insertFast(100);
        int dbreccnt = db.countRecords();
        //Log.d("SplashAfterDBCreate", "made it");
        //Log.d("AppLoadrunner ", "Loadrunner record count " + dbreccnt);

        Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(LrToolBar);
        //getSupportActionBar().setDisplayShowHomeEnabled(true);
        //LrToolBar.setNavigationContentDescription(getResources().getString(R.string.app_name));
        setContentView(new TableMainLayout(this));

TableMainLayout start:

package com.Table;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
//import android.database.Cursor;

import com.loadrunner.DatabaseHandler;
import com.loadrunner.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


public class TableMainLayout extends RelativeLayout {

    public final String TAG = "TableMainLayout.java";

3条回答
叼着烟拽天下
2楼-- · 2019-09-05 13:50

Try this:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="windowActionBar">false</item> //see this line
    <item name="android:windowNoTitle">true</item>
</style>

Try setting <item name="windowActionBar">false</item>

I hope this helps!

查看更多
倾城 Initia
3楼-- · 2019-09-05 14:07

Make your Toolbar layout like thie:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:background="#2196f3"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:minHeight="200dp" />

and in your Activity code replace the Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar); with the:

Toolbar toolbar = LayoutInflater.from(this).inflate(R.layout.toolbar_layout_name, null)
setSupportActionBar(toolbar);

Hope it works.

查看更多
聊天终结者
4楼-- · 2019-09-05 14:09

I have maybe the answer : you just get your toolbar before setContentView() method... Just try this :

    setContentView(new TableMainLayout(this));
    Toolbar LrToolBar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(LrToolBar);
    //getSupportActionBar().setDisplayShowHomeEnabled(true);
    //LrToolBar.setNavigationContentDescription(getResources().getString(R.string.app_name));

Hope it will help you

查看更多
登录 后发表回答