如何将菜单和设置选项添加到Android的自定义标题栏?

时间:2015-01-09 07:20:42

标签: android

我的要求是增加标题栏的高度,但我不确定这是否可行。如果是我怎么能增加它的高度所以我会有更多的空间。如果不是,我需要在我的自定义标题栏中添加一个菜单选项(汉堡风格)。

custom_title_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#77b48e"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="29dp"
            android:layout_height="28dp"
            android:maxWidth="1dp"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/title_bar_viewname"
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:layout_weight="0.07"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="10sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
    }

2 个答案:

答案 0 :(得分:17)

  1. 更改 MainActivity.java ,如下所示:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 
            setContentView(R.layout.activity_main);
    
           actionBar = getActionBar();
            ColorDrawable colorDrawable = new ColorDrawable(
                    Color.parseColor("#373836"));
            actionBar.setBackgroundDrawable(colorDrawable);
    
        }
    

    对于例如:,现在您的操作栏将显示如下:

    enter image description here

  2. 然后,如果您需要在操作栏中添加setting menu项目。

    MainActivity,java

    中添加以下代码
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       switch (item.getItemId()) 
       {
         case R.id.search:
          //your code here
            return true;
         default:
            return super.onOptionsItemSelected(item);
       }
    }   
    

    然后在 res / menu / main.xml:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" >
    
        <item
            android:id="@+id/search"
            android:icon="@drawable/ic_action_settings"
            android:showAsAction="always"
            android:title="text"/>
    
    </menu>
    

    现在您可以在操作栏中看到设置图标

    enter image description here

  3. 最后,如果你必须增加操作栏高度

    清单

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" > --->set a theme 
    
    ...
    </application>
    

    然后在 Styles.xml

    <resources>
    
        <style name="AppBaseTheme" parent="android:Theme.Light"></style>
    
        <style name="AppTheme" parent="AppBaseTheme">
            <item name="android:actionBarSize">80dip</item>
        </style>
    
    </resources>  
    

    现在操作栏大小增加到我们给出的大小 在风格。

    enter image description here

答案 1 :(得分:2)

随着Android L的发布,一个新视图已经发布:工具栏。工具栏为您设置动作栏的样式提供了更大的灵活性。它也可以通过appCompat库用于较低的Android版本。

  

在此版本中,Android引入了一个新的工具栏小部件。这是Action Bar模式的概括,为您提供更多控制和灵活性。工具栏是层次结构中的视图,与其他视图一样,可以更轻松地与其余视图交错,为其设置动画并对滚动事件做出反应。您也可以将其设置为活动的操作栏,这意味着您的标准选项菜单操作将显示在其中。

您可以使用工具栏更改操作栏的高度,只需将layout_height属性设置为所需的高度。

有关如何使用它的更多信息:

http://android-developers.blogspot.be/2014/10/appcompat-v21-material-design-for-pre.html

文档:

http://developer.android.com/reference/android/widget/Toolbar.html