以编程方式更改自定义操作栏的标题

时间:2015-05-04 20:25:42

标签: android android-actionbar

我无法以编程方式更改自定义操作栏的标题,也无法将配置文件图像添加到操作栏。我无法理解我的实现错误。以下是我的代码:

actionbar_home

<RelativeLayout
    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:background="@color/tabbackground"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <ImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profileImageActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:visibility="visible"
        app:border_width="1dp"
        app:border_color="#FFFFFF"
        android:layout_alignParentLeft="true"

      />


    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:maxLines="1"
        android:singleLine="true"
        android:text="Home"
        android:textColor="@color/white"
        android:textSize="22sp"/>
</RelativeLayout>

代码 -

  protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mCamera=(ImageButton)findViewById(R.id.menu_camera);
            mMimic=(ImageButton)findViewById(R.id.menu_mimic);
            mMeme=(ImageButton)findViewById(R.id.menu_makememe);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.left_drawer);
            mMenuButton = (ImageButton) findViewById(R.id.menu_menu);
            mMenuBackground=(View)findViewById(R.id.menu_white_background);
            header=getLayoutInflater().inflate(R.layout.header, null);
            mProfilePic = (ImageView) findViewById(R.id.circleView);
            mProfileName = (TextView) findViewById(R.id.userName);
            mmenuCamera=(View)findViewById(R.id.menu_camera_box);
            mmenuMimic=(View)findViewById(R.id.menu_mimiclayout);
            mmenuMeme=(View)findViewById(R.id.menu_make_meme);
            transparent=(View)findViewById(R.id.blacktreansparent);

            LayoutInflater mInflater = LayoutInflater.from(this);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            View mCustomView = mInflater.inflate(R.layout.actionbar_home, null);

            profileImage=(ImageView)mCustomView.findViewById(R.id.profileImageActionBar);
            mFaceBookActionBarPicture = (ProfilePictureView)mCustomView.findViewById(R.id.faceboolUserProfilePicture);

            actionBarTitle=(TextView)mCustomView.findViewById(R.id.actionbar_title);
            getSupportActionBar().setCustomView(R.layout.actionbar_home);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayShowCustomEnabled(true);
actionBarTitle.setText("PG");
            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(android.R.color.transparent)
                    .showImageForEmptyUri(android.R.color.transparent)
                    .showImageOnFail(android.R.color.transparent)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();
            handler= new Handler();
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                    getApplicationContext())
                    .defaultDisplayImageOptions(options)
                    .memoryCache(new WeakMemoryCache())
                    .discCacheSize(100 * 1024 * 1024).build();
            ImageLoader.getInstance().init(config);

          background=(ImageView)findViewById(R.id.backgroundView);
            mFaceBookProfilePictureView = (ProfilePictureView) findViewById(R.id.faceboolUserProfilePicture);
            try {
                setProfile(mProfilePic, mProfileName, mFaceBookProfilePictureView);
            } catch (IOException e) {
                e.printStackTrace();
            }

            mDrawerList.setAdapter(new CustomAdapter(this, mdrawerTitles));
            setupAnimations();
           // getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);


             mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                   @Override
                                                   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                    if(position==0)
                                                    {

                                                        Intent viewAboutVideoIntent = new Intent(getApplicationContext(), YouTubeActivity_.class);
                                                        startActivity(viewAboutVideoIntent);
                                                    }
                                                       else if(position==1)
                                                    {
                                                        GApplication.getInstance().triggerShareApp(MainActivity.this);
                                                    }
                                                       else if(position==2)
                                                    {
                                                       GApplication.getInstance().connectUsFaceBookPage(MainActivity.this);
                                                    }
                                                    else if(position==3)
                                                    {
                                                        Session session = Session.getActiveSession();
                                                        if (session != null)
                                                        {
                                                            Session.getActiveSession().closeAndClearTokenInformation();
                                                            Session.getActiveSession().close();
                                                            Session.setActiveSession(null);
                                                        }
                                                        else
                                                        {
                                                            session = new Session.Builder(GApplication.getInstance()).setApplicationId(getResources().getString(R.string.app_id)).build();
                                                            if (session != null)
                                                            {
                                                                Session.setActiveSession(session);
                                                                session.closeAndClearTokenInformation();
                                                                session.close();
                                                                Session.setActiveSession(null);
                                                            }
                                                        }

                                                        Intent i=new Intent(getApplicationContext(),IntroVideoActivity_.class);
                                                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                                        G.FACEBOOK_USER_PROFILE_PIC.write(null);
                                                        G.FACEBOOK_USER_NAME.write(null);
                                                        G.USER_NAME.write(null);
                                                        G.USER_EMAILID.write(null);
                                                        G.GOOGLEPLUS_USER_PROFILE_PIC.write(null);
                                                        G.FIRSTTIME_LAUNCH.write(true);
                                                        startActivity(i);
                                                        finish();
                                                    }
                                                   }
                                               });



            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                    R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);

                    invalidateOptionsMenu();
                }

                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);

                    invalidateOptionsMenu();
                }
            };

            mDrawerLayout.setDrawerListener(mDrawerToggle);

            final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
            pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
            final PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
            tabs.setViewPager(pager);

            tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    tabs.setNextFocusRightId(position);
                    switch(position) {
                        case 0:


                            actionBarTitle.setText("Home");

                            break;
                        case 1:


                            actionBarTitle.setText("PG");
                            break;
                        case 2:


                            actionBarTitle.setText("AG");
                            break;
                        case 3:


                            actionBarTitle.setText("VG");
                            break;
                        case 4:

                            actionBarTitle.setText("MG");
                            break;
                        case 5:


                            actionBarTitle.setText("Ch");
                            break;
                        case 6:


                            actionBarTitle.setText("Gi");
                            break;
                        case 7:


                            actionBarTitle.setText("Fl");
                            break;
                    }

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }

            });
            mIsMenuOpen = false;
            mMenuButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mIsMenuOpen)
                    {
                        transparent.setVisibility(View.GONE);
                        mMenuButton.startAnimation(mCloseAnimation);

                        mmenuCamera.startAnimation(mSlideOutAnimation);
                        mmenuMimic.startAnimation(mSlideOutAnimation);
                        mmenuMeme.startAnimation(mSlideOutAnimation);

                        mMenuBackground.startAnimation(mFadeOutAnimation);


                        mIsMenuOpen = false;
                    }
                    else
                    {
                        transparent.setVisibility(View.VISIBLE);
                        mMenuButton.startAnimation(mOpenAnimation);

                        mmenuCamera.startAnimation(mSlideInAnimation);
                        mmenuMimic.startAnimation(mSlideInAnimation);
                        mmenuMeme.startAnimation(mSlideInAnimation);

                        mMenuBackground.startAnimation(mFadeInAnimation);


                        mIsMenuOpen = true;
                    }
                }
            });

        }

1 个答案:

答案 0 :(得分:2)

在您的情况下,您要对视图进行两次充气。

在这一行:

 View mCustomView = mInflater.inflate(R.layout.actionbar_home, null);

这一行:

getSupportActionBar().setCustomView(R.layout.actionbar_home);

因此,当访问“actionbarTitle”时,您引用的是第一次通货膨胀,而不是附加到ActionBar的实际视图(因为方法setCustomView(int id)会使视图本身膨胀,请参阅docs)。

要访问实际的标题,您需要使用:

getSupportActionBar().setCustomView(mCustomView);

或者在将资源设置为ActionBar之后初始化“mCustomView”视图:

getSupportActionBar().setCustomView(R.layout.actionbar_home);
mCustomView = getSupportActionBar().getCustomView();