准备自定义操作栏

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

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

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

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

我的电流输出:

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

<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)如何使这个动作条要对所有的活动,而不是单一的活动,我现在的工作充气。

添加下面的代码到你的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;

}

和9补丁这里

以上是 准备自定义操作栏 的全部内容, 来源链接: utcz.com/qa/258627.html

回到顶部