简体   繁体   中英

Hide title in action bar and display icons

I am trying to have a title bar that displays various icons that upon click would direct the user into their appropriate activity page. Unfortunately, I have encountered the following issues: -Hiding the activity title -Icons not being displayed on the activity bar, but is only shown as a droplist

Below is the code I am using in android manifest

 <activity
            android:name="com.dooba.beta.MoodActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Holo">
         <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

Below is the menu xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/pageExperience"
          android:icon="@drawable/fork_spoon3"
          android:title="@string/action_experience"/>

    <item android:id="@+id/pageMessaging"
          android:icon="@drawable/messageicon"
          android:title="@string/action_messaging"/>

    <item android:id="@+id/pageEventsBooking"
          android:icon="@drawable/fork_spoon3"
          android:title="@string/action_book"/>
    <item android:id="@+id/pageProfile"
          android:icon="@drawable/fork_spoon3"
          android:title="@string/action_profile" />
     <item android:id="@+id/pageReport"
          android:icon="@drawable/fork_spoon3"
          android:title="@string/action_report" />
      <item android:id="@+id/pageAbout"
          android:icon="@drawable/fork_spoon3"
          android:title="@string/action_about" />
</menu>

Below is the activity code that calls the menu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.pageExperience:
                openPageExperience();
                return true;
            case R.id.pageMessaging:
                openPageMessage();
                return true;

            case R.id.pageEventsBooking:
                openPageBook();
                return true;

            case R.id.pageProfile:
                openPageProfile();
                return true;

            case R.id.pageReport:
                openPageReport();
                return true;

            case R.id.pageAbout:
                openPageAbout();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void openPageAbout() {
        // TODO Auto-generated method stub

    }

    private void openPageReport() {
        // TODO Auto-generated method stub

    }

    private void openPageProfile() {
        // TODO Auto-generated method stub

    }

    private void openPageBook() {
        // TODO Auto-generated method stub

    }

    private void openPageMessage() {
        // TODO Auto-generated method stub

    }

    private void openPageExperience() {
        // TODO Auto-generated method stub

    }

}

Below is the strings xml file that refers to the title icons string name

   <string name="action_experience">Experience</string>
    <string name="action_messaging">Messaging</string>
     <string name="action_book">Book</string>
    <string name="action_profile">Profile</string>
    <string name="action_report">"Report</string>
        <string name="short_name"></string>

    <string name="action_about">About</string>

Update: I have recieved the following issues when trying to implement the code ActionBar ab = getActionBar(); getActionBar Type mismatch: cannot convert from android.app.ActionBar to android.support.v7.app.ActionBar

customActionBarView cannot be resolved to a variable main_activity_actions cannot be resolved or is not a field

The activity code

public class MoodActivity extends Activity {

    @Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first
        setCustomActionBarView();

    }

    private void setCustomActionBarView() {
        // TODO Auto-generated method stub
        ActionBar ab = getActionBar();
        ab.setDisplayHomeAsUpEnabled(false);
        ab.setDisplayShowTitleEnabled(false);
        ab.setDisplayShowCustomEnabled(true);
        if(customActionBarView == null){
            LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            customActionBarView = inflator.inflate(R.layout.main_activity_actions,null);
        }
        ab.setCustomView(customActionBarView);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mood);

        final TextView teating = (TextView) this.findViewById(R.id.tdinning);
        teating.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
            }
        });

        final ImageView ieating = (ImageView) this.findViewById(R.id.idinning);
        ieating.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
            }
        });

        final TextView tdrinks = (TextView) this.findViewById(R.id.tcasual);
        tdrinks.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
            }
        });

        final ImageView idrinks = (ImageView) this.findViewById(R.id.icasual);
        idrinks.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
            }
        });

        final TextView tshows = (TextView) this.findViewById(R.id.tshows);
        tshows.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class));
            }
        });

        final ImageView ishows = (ImageView) this.findViewById(R.id.ishows);
        ishows.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class));
            }
        });

        final TextView tarts = (TextView) this.findViewById(R.id.tculture);
        tarts.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class));
            }
        });

        final ImageView iarts = (ImageView) this.findViewById(R.id.iculture);
        iarts.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class));
            }
        });

        final Button viewall = (Button) this.findViewById(R.id.brandom);
        viewall.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
            }
        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_actions, menu);


        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.pageExperience:
                openPageExperience();
                return true;
            case R.id.pageMessaging:
                openPageMessage();
                return true;

            case R.id.pageEventsBooking:
                openPageBook();
                return true;

            case R.id.pageProfile:
                openPageProfile();
                return true;

            case R.id.pageReport:
                openPageReport();
                return true;

            case R.id.pageAbout:
                openPageAbout();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void openPageAbout() {
        // TODO Auto-generated method stub

    }

    private void openPageReport() {
        // TODO Auto-generated method stub

    }

    private void openPageProfile() {
        // TODO Auto-generated method stub

    }

    private void openPageBook() {
        // TODO Auto-generated method stub

    }

    private void openPageMessage() {
        // TODO Auto-generated method stub

    }

    private void openPageExperience() {
        // TODO Auto-generated method stub

    }

}

Update I now have the follow issue with the following line: getSupportActionBar(); it gives me the following issue: The method getSupportActionBar() is undefined for the type MoodActivity

@Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first
        setCustomActionBarView();

    }

    private void setCustomActionBarView() {
        // TODO Auto-generated method stub
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(false);
        ab.setDisplayShowTitleEnabled(false);
        ab.setDisplayShowCustomEnabled(true);
        View customActionBarView;
        if(customActionBarView == null){
            LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            customActionBarView = inflator.inflate(R.layout.main_activity_actions,null);
        }
        ab.setCustomView(customActionBarView);
    }
ActionBar ab = getActionBar();
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
if(customActionBarView == null){
    LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    customActionBarView = inflator.inflate(R.layout.my_custom_view,null);
}
ab.setCustomView(customActionBarView);

Here is what it does:

setDisplayHomeAsUpEnabled(boolean showHomeAsUp)  

is used when you are using a navigation drawer and you want the user to go up a level in your view hierarchy.

setDisplayShowTitleEnabled(boolean showTitle);  

is used to decide whether or not the activity's title should be displayed

setDisplayShowCustomEnabled(boolean showCustom);  

is used to decide if you want to display a custom view for your ActionBar

Then, you can use customActionBarView.findViewById() to retrieve your icons and add listeners to them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM