2013-08-01 31 views
5

Tôi đang cố gắng sử dụng ActionBar Compat mới được cung cấp trong lib hỗ trợ-v7 cuối cùng. Sử dụng các mẫu trong SuppportV7Demos, khi tôi chạm vào "DISPLAY_USE_LOGO", biểu tượng không thay đổi.ActionBar Compat không hiển thị logo trên Android 2.3.3

Vì tôi không thể làm cho nó hoạt động trong ứng dụng của riêng tôi, có ai đã đạt được điều này không?

ActionBarDisplayOptions.java 

package com.example.android.supportv7.app; 

public class ActionBarDisplayOptions extends ActionBarActivity 
    implements View.OnClickListener, ActionBar.TabListener { 
private View mCustomView; 
private ActionBar.LayoutParams mCustomViewLayoutParams; 

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

    findViewById(R.id.toggle_home_as_up).setOnClickListener(this); 
    findViewById(R.id.toggle_show_home).setOnClickListener(this); 
    findViewById(R.id.toggle_use_logo).setOnClickListener(this); 
    findViewById(R.id.toggle_show_title).setOnClickListener(this); 
    findViewById(R.id.toggle_show_custom).setOnClickListener(this); 
    findViewById(R.id.toggle_navigation).setOnClickListener(this); 
    findViewById(R.id.cycle_custom_gravity).setOnClickListener(this); 
    findViewById(R.id.toggle_visibility).setOnClickListener(this); 

    // Configure several action bar elements that will be toggled by display options. 
    mCustomView = getLayoutInflater().inflate(R.layout.action_bar_display_options_custom, null); 
    mCustomViewLayoutParams = new ActionBar.LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    final ActionBar bar = getSupportActionBar(); 
    bar.setCustomView(mCustomView, mCustomViewLayoutParams); 
    bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this)); 
    bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this)); 
    bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this)); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.display_options_actions, menu); 
    return true; 
} 

@Override 
public boolean onSupportNavigateUp() { 
    finish(); 
    return true; 
} 

@Override 
public void onClick(View v) { 
    final ActionBar bar = getSupportActionBar(); 
    int flags = 0; 
    switch (v.getId()) { 
     case R.id.toggle_home_as_up: 
      flags = ActionBar.DISPLAY_HOME_AS_UP; 
      break; 
     case R.id.toggle_show_home: 
      flags = ActionBar.DISPLAY_SHOW_HOME; 
      break; 
     case R.id.toggle_use_logo: 
      flags = ActionBar.DISPLAY_USE_LOGO; 
      break; 
     case R.id.toggle_show_title: 
      flags = ActionBar.DISPLAY_SHOW_TITLE; 
      break; 
     case R.id.toggle_show_custom: 
      flags = ActionBar.DISPLAY_SHOW_CUSTOM; 
      break; 
     case R.id.toggle_navigation: 
      bar.setNavigationMode(
        bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD 
          ? ActionBar.NAVIGATION_MODE_TABS 
          : ActionBar.NAVIGATION_MODE_STANDARD); 
      return; 
     case R.id.cycle_custom_gravity: { 
      ActionBar.LayoutParams lp = mCustomViewLayoutParams; 
      int newGravity = 0; 
      switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 
       case Gravity.LEFT: 
        newGravity = Gravity.CENTER_HORIZONTAL; 
        break; 
       case Gravity.CENTER_HORIZONTAL: 
        newGravity = Gravity.RIGHT; 
        break; 
       case Gravity.RIGHT: 
        newGravity = Gravity.LEFT; 
        break; 
      } 
      lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | newGravity; 
      bar.setCustomView(mCustomView, lp); 
      return; 
     } 
     case R.id.toggle_visibility: 
      if (bar.isShowing()) { 
       bar.hide(); 
      } else { 
       bar.show(); 
      } 
      return; 
    } 

    int change = bar.getDisplayOptions()^flags; 
    bar.setDisplayOptions(change, flags); 
} 

@Override 
public void onTabSelected(Tab tab, FragmentTransaction ft) { 
} 

@Override 
public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
} 

@Override 
public void onTabReselected(Tab tab, FragmentTransaction ft) { 
} 
} 

và: action_bar_display_options.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
<LinearLayout android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 
    <Button android:id="@+id/toggle_home_as_up" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/toggle_home_as_up" /> 
    <Button android:id="@+id/toggle_show_home" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/toggle_show_home" /> 
    <Button android:id="@+id/toggle_use_logo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/toggle_use_logo" /> 
    <Button android:id="@+id/toggle_show_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/toggle_show_title" /> 
    <Button android:id="@+id/toggle_show_custom" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/toggle_show_custom" /> 
    <Button android:id="@+id/toggle_navigation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/toggle_navigation" /> 
    <Button android:id="@+id/cycle_custom_gravity" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/cycle_custom_gravity" /> 
    <Button android:id="@+id/toggle_visibility" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/toggle_visibility" /> 
</LinearLayout> 
</ScrollView> 

Trả lời

20

Trong phiên bản ActionBarSherlock của ứng dụng của tôi, tôi đã thiết lập các biểu tượng và logo trong biểu hiện. Sau khi thay đổi thành ActionBarCompat, logo không hiển thị trên thanh hành động trên Android 2.2, 2.3.x.

Để thực hiện các chương trình biểu tượng tôi đặt nó trong mã

ActionBar ab = getSupportActionBar(); 
ab.setLogo(R.drawable.logo); 

và loại bỏ 'Android: logo = "@ drawable/logo_icon"' từ manifest.

Đã thử nghiệm trên Android 2.2, 2.3.3 và 4.1.2.

+2

nó hoạt động ngay cả khi không loại bỏ các 'android: logo = '@ drawable/logo_icon' từ manifest . – JulienC

+0

nó làm việc cho tôi! – ingyesid

7

Đặt biểu trưng ở AndroidManifest.xml không cập nhật thanh tác vụ trong các phiên bản Android cũ hơn (trước API 9), vì không có cách nào để lấy giá trị đó khi chạy trong các phiên bản Android đó.

Để thay đổi biểu tượng để một logo trong tất cả các phiên bản Android bạn có thể tạo kiểu thanh hành động trong /res/values.xml:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"> 
    <item name="actionBarStyle">@style/Widget.AppActionBar</item> 
</style> 
<style name="Widget.AppActionBar" parent="@style/Widget.AppCompat.ActionBar"> 
    <item name="logo">@drawable/ic_logo</item> 
</style> 
Các vấn đề liên quan