ImageView不会出现在导航抽屉工具栏中

时间:2016-08-21 19:24:48

标签: android android-actionbar

我使用导航抽屉,我希望我的应用程序的徽标出现在导航抽屉工具栏的中心。我已经在线尝试了很多解决方案,但我的徽标出现在预览中,但是当我运行应用时,它不在我的工具栏中。 这是我的App Bar布局代码:

<?xml version="1.0" encoding="utf-8"?>

<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:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/logoActionBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@drawable/actionbarlogo" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_home_customer" />

这是我的导航抽屉的主要活动:

    public class BaseActivity extends AppCompatActivity {


    private DrawerLayout mDrawer;
    private Toolbar toolbar;
    private NavigationView nvDrawer;
    private ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_customer);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        //  toolbar.setLogo(R.drawable.actionbarlogo);
//
        setSupportActionBar(toolbar);

        Constants cnst = new Constants();
        cnst.getNearbyusers(33.522212, 73.091904);

        // getSupportActionBar().setIcon(R.drawable.logo_final);
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        nvDrawer = (NavigationView) findViewById(R.id.nav_view);
        setupDrawerContent(nvDrawer);
        drawerToggle = setupDrawerToggle();
        mDrawer.addDrawerListener(drawerToggle);
        //  getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        View hView = nvDrawer.getHeaderView(0);
        TextView nav_username = (TextView) hView.findViewById(R.id.fullNameUser);
        TextView nav_emailuser = (TextView) hView.findViewById(R.id.emailUser);
        ImageView nav_profilepic = (ImageView) hView.findViewById(R.id.ProfilePhoto);

        Fragment home = new HomeFragment();
        FragmentManager FM = getSupportFragmentManager();
        FM
                .beginTransaction()
                .replace(R.id.container_layout, home)
                .commit();

        SharedPreferences prefs = getApplicationContext().getSharedPreferences("HouseKeepingtg", Context.MODE_PRIVATE);

        String username = prefs.getString("fName", null);
        String email = prefs.getString("Email", null);
        String profilepic = prefs.getString("Profilepic", null);
        nav_username.setText(username);
        nav_emailuser.setText(email);
        if (profilepic != null)
            nav_profilepic.setImageBitmap(decodeBase64(profilepic));

        Log.v("UserData", username + email);


    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        drawerToggle.onConfigurationChanged(newConfig);
    }

    private ActionBarDrawerToggle setupDrawerToggle() {
        return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        selectDrawerItem(menuItem);
                        return true;
                    }
                });
    }

    public void selectDrawerItem(MenuItem menuItem) {
        Handler handler = new Handler();
        // Create a new fragment and specify the fragment to show based on nav item clicked
        Fragment fragment = null;
        Class fragmentClass;
        switch (menuItem.getItemId()) {
            case R.id.nav_display_worker_list:
                fragmentClass = DisplayWorkersActivity.class;
                break;
            case R.id.nav_map:
                fragmentClass = MapsActivity.class;
                break;
            case R.id.nav_home:
                fragmentClass = HomeFragment.class;
            case R.id.nav_settings:
                fragmentClass = SettingsFragment.class;
                break;
//            case R.id.nav_update_profile:
//                    Intent intent = new Intent(BaseActivity.this, SignupRetailer.class);
//                    startActivity(intent);
//
//                break;
            default:
                fragmentClass = HomeFragment.class;
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Insert the fragment by replacing any existing fragment
        final FragmentManager fragmentManager = getSupportFragmentManager();
        final Fragment finalFragment = fragment;
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                fragmentManager.beginTransaction().replace(R.id.container_layout, finalFragment).commit();

            }

        }, 250);


        // Highlight the selected item has been done by NavigationView
        menuItem.setChecked(true);
        // Set action bar title
        setTitle(menuItem.getTitle());
        // Close the navigation drawer
        mDrawer.closeDrawers();


    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_customer, 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);
//    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public static Bitmap decodeBase64(String input) {
        byte[] decodedBytes = Base64.decode(input, 0);
        return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
    }


    public void clearStack() {
/*
* Here we are clearing back stack fragment entries
*/
        int backStackEntry = getSupportFragmentManager().getBackStackEntryCount();

        if (backStackEntry > 0) {
            for (int i = 0; i < backStackEntry; i++) {
                getSupportFragmentManager().popBackStackImmediate();
            }
        }

/*
* Here we are removing all the fragment that are shown here
*/
        if (getSupportFragmentManager().getFragments() != null && getSupportFragmentManager().getFragments().size() > 0) {
            for (int i = 0; i < getSupportFragmentManager().getFragments().size(); i++) {
                Fragment mFragment = getSupportFragmentManager().getFragments().get(i);
                if (mFragment != null) {
                    getSupportFragmentManager().beginTransaction().remove(mFragment).commit();
                }
            }
        }
    }


}

1 个答案:

答案 0 :(得分:0)

虽然工具栏是ViewGroup,但您不会像使用XML格式那样添加子视图。你要么

  • 将特定的XML属性添加到实际工具栏,例如android.support.v7.appcompat:logo和其他

  • 使用代码向其中添加视图,例如setLogo()和其他工具栏功能。

您可以在文档中阅读所有内容。