2013-07-25 48 views
1

下面是我当前设置的屏幕截图。Android自定义操作栏删除图标

enter image description here

我创建了一个自定义操作栏视图,我在下面的代码设置,并且我想是两个图像,一个在左边的标题栏排列,其他的右对齐。

问题是,当我隐藏应用程序图标时,它只会隐藏它,不会删除它,因此左边的空白。我发现了一些其他的SO问题,展示了如何删除图标,但这也删除了我想保留的选项卡。

任何人都可以为我提供解决方案吗?

从我的onCreate()函数:

final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  

     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflater.inflate(R.layout.action_bar_title, null); 

     View homeIcon = findViewById(android.R.id.home); 
     ((View) homeIcon.getParent()).setVisibility(View.GONE); 
     ((View) homeIcon).setVisibility(View.GONE);   

     actionBar.setDisplayShowCustomEnabled(true); 
     actionBar.setDisplayShowTitleEnabled(false); 
     actionBar.setDisplayShowHomeEnabled(true); 
     actionBar.setCustomView(v); 

我的XML定制布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    android:gravity="fill_horizontal" 
    android:layout_marginLeft="0dp" 
    android:orientation="horizontal" 
    >  

    <ImageView android:id="@+id/title_img_left" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true"      
        android:src="@drawable/test" /> 
    <ImageView android:id="@+id/title_img_right" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" 
        android:src="@drawable/test" /> 


</RelativeLayout> 

我使用Holo.Light.DarkActionBar主题,我相信。

+0

这没有iPhone。操作栏中的按钮应该位于右侧。 – bofredo

+0

所以,试试用你自己的风格继承sherlock widget吧 – deadfish

+0

@bofredo我不明白iPhone与任何东西有什么关系。他们不是按钮,他们是图像,这就是我希望他们在Android的设计 – TomP89

回答

0

因此,如果我理解你正在努力实现的目标,为什么不在你的左侧添加一个MenuItem的自定义图像,并为你设置actionBarLogo的图像,你想在右侧。之后,只是重写主页按钮单击,我想你会实现相同的东西,你可以用自定义视图创建,没有ActionBar左侧的填充。

附:如果我理解你错了,请提供一些关于它应该如何的信息。

0

该代码正在工作!

ActionBar actionBar = getSherlockActivity().getSupportActionBar(); 

     View customNav = LayoutInflater.from(getActivity()).inflate(R.layout.custom_action_bar_view, null); 
     ImageButton imageButton = (ImageButton) customNav.findViewById(R.id.imageButton); 

     ActionBar.LayoutParams lp = new ActionBar 
      .LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 
     actionBar.setCustomView(customNav, lp); 
     actionBar.setDisplayShowCustomEnabled(true); 
     actionBar.setDisplayShowTitleEnabled(false); 
     actionBar.setDisplayShowHomeEnabled(false);