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

Jac*_*ack 9 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>
Run Code Online (Sandbox Code Playgroud)

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);
    }
Run Code Online (Sandbox Code Playgroud)

Luf*_*ffy 18

  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);
    
        }
    
    Run Code Online (Sandbox Code Playgroud)

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

    在此输入图像描述

  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);
       }
    }   
    
    Run Code Online (Sandbox Code Playgroud)

    然后在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>
    
    Run Code Online (Sandbox Code Playgroud)

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

    在此输入图像描述

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

    清单中:

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" > --->set a theme 
    
    ...
    </application>
    
    Run Code Online (Sandbox Code Playgroud)

    然后在Styles.xml中:

    <resources>
    
        <style name="AppBaseTheme" parent="android:Theme.Light"></style>
    
        <style name="AppTheme" parent="AppBaseTheme">
            <item name="android:actionBarSize">80dip</item>
        </style>
    
    </resources>  
    
    Run Code Online (Sandbox Code Playgroud)

    现在,操作栏大小增加到我们在样式中给出的大小.

    在此输入图像描述