【问题标题】:Preparing custom action bar准备自定义操作栏
【发布时间】:2013-05-27 06:10:47
【问题描述】:

我正在尝试完全像下图一样。

但是我现在可以得到下面的。这里我有两个问题。

1) 如何让这个actionbar 用于所有活动,而不是我现在正在膨胀的单个活动。

2) 上面的图片看起来像是提升的布局栏,但我的看起来像一个普通的面板。有人可以帮我实现这些吗?代码 sn-ps 表示赞赏。

我目前的输出:

我的布局代码膨胀到操作栏:

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

活动代码sn-ps:

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);
    }

@drawable/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>

【问题讨论】:

    标签: android actionbarsherlock customization


    【解决方案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;
    }
    

    这里有 9 个补丁

    【讨论】:

    • 提升的布局栏是什么意思?请解释一下,只有我可以提供帮助。
    • 感谢您的回复。但是根据您的代码,操作项和主页图标是根据操作栏样式显示的,我的意思是只有在有可用空间的情况下。如果我错了,请纠正我。对于第二个问题,如您所见,与操作栏下方的屏幕相比,操作栏布局看起来似乎有所提升。在我帖子的第一张图片中看得很清楚。
    • 您可以将按钮设置为始终显示为 .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    • 尝试在操作栏中交换渐变颜色。然后它会得到一个提升的外观(较浅的颜色总是应该在开始和最后变暗)。我正确吗?还是要在操作栏下方添加阴影?
    • 尝试使用我附加的图片作​​为操作栏背景
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多