簡體   English   中英

在操作欄中隱藏標題並顯示圖標

[英]Hide title in action bar and display icons

我正在嘗試有一個標題欄,顯示各種圖標,點擊后會將用戶引導到他們適當的活動頁面。 不幸的是,我遇到了以下問題:-隱藏活動標題-圖標未顯示在活動欄上,而僅顯示為下拉列表

下面是我在 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>

下面是菜單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>

下面是調用菜單的活動代碼

@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

    }

}

下面是引用標題圖標字符串名稱的字符串 xml 文件

   <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>

更新:我在嘗試實現代碼 ActionBar ab = getActionBar(); 時收到以下問題; getActionBar 類型不匹配:無法從 android.app.ActionBar 轉換為 android.support.v7.app.ActionBar

customActionBarView 無法解析為變量 main_activity_actions 無法解析或不是字段

活動代碼

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

    }

}

更新我現在有以下問題:getSupportActionBar(); 它給了我以下問題:方法 getSupportActionBar() 未定義為 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);

這是它的作用:

setDisplayHomeAsUpEnabled(boolean showHomeAsUp)  

當您使用導航抽屜並且您希望用戶在視圖層次結構中上升一個級別時使用。

setDisplayShowTitleEnabled(boolean showTitle);  

用於決定是否應該顯示活動的標題

setDisplayShowCustomEnabled(boolean showCustom);  

用於決定是否要為ActionBar顯示自定義視圖

然后,您可以使用customActionBarView.findViewById()來檢索您的圖標並向它們添加偵聽器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM