2013-05-27 69 views
3

我想获得完全像下面的图片。准备自定义操作栏

enter image description here

但我能得到下面的一个现在。这里我有两个问题。

1)如何使这actionbar工作在所有活动,而不是我现在正在膨胀的单一活动。

2)上述图像好像升高的布局条,但我看起来像一个正常面板。有人可以帮助我如何实现这些?代码片断表示赞赏。

我的电流输出:

enter image description here

我的布局代码膨胀到动作条:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:background="@drawable/ab_customshape" > 

    <ImageButton 
      android:id="@+id/homeicon" 
      android:layout_width="40sp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:src="@drawable/ic_launcher" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" > 
    </ImageButton> 

    <TextView 
     android:id="@+id/title_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="My Title" 
     android:layout_centerVertical="true" 
     android:textColor="#ffffff" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 
    <ImageButton 
     android:id="@+id/rightbutton" 
      android:layout_width="40sp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:src="@drawable/ic_launcher" 
      android:background="@android:color/transparent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
     /> 

</RelativeLayout> 

活动代码片断:

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

     initActionBar(); 
    } 

    private void initActionBar() { 
     mActionBar = getSupportActionBar(); 
     mActionBar.setDisplayShowHomeEnabled(false); 
     mActionBar.setDisplayShowTitleEnabled(false); 
     mInflater = LayoutInflater.from(this); 
     mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 
     mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text); 
     mTitleTextView.setText("My Own Title"); 
     mActionBar.setCustomView(mCustomView); 
     mActionBar.setDisplayShowCustomEnabled(true); 
    } 

@绘制/ ab_customshape:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <gradient 
     android:angle="-90" 
     android:endColor="#DD2ECCFA" 
     android:startColor="#DD000000" 
     android:type="linear" /> 

</shape> 

回答

1

1)如何使这个动作条要对所有的活动,而不是单一的活动,我现在的工作充气。

添加下面的代码到你的style.xml。

<style name="Theme.Styled" parent="Theme.Sherlock.Light"> 
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item> 
    <item name="android:actionModeCloseDrawable">@drawable/collapse_button</item> 
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item> 
</style> 
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse"> 
    <item name="background">@drawable/ab_customshape</item> 
    <item name="android:background">@drawable/ab_customshape</item> 

</style> 

然后将这个选项设为您的清单喜欢你的应用程序的主题,

<application 
    android:allowBackup="true" 
    android:icon="@drawable/app_icon" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.Styled" > //Here we set the theme 

并尝试按钮添加到您的动作条这样的,

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 


    menu.add(0, HOMEBUTTON, 0, "Home").setIcon(R.drawable.home_button) 

    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 

    return true; 
} 

enter image description here

和9补丁这里enter image description here

+0

你是什么意思的提升布局栏?请解释一下,只有我可以帮忙。 –

+0

感谢您的回复。但根据您的代码,操作项目和主页图标根据操作栏样式显示,我的意思是只有有空房间。如果我错了,请纠正我。关于第二个问题,你可以看到,在动作条的布局似乎它比动作条下面的画面时看起来升高。在我的帖子的第一张图片中清楚地看到。 – Spike

+0

您可以设置按钮,以显示总是喜欢,.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); –