如何在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);
    }
史蒂夫
  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);
    
        }
    

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

    在此处输入图片说明

  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>
    

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

    在此处输入图片说明

  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>  
    

    现在,操作栏的大小已增加到我们在样式中指定的大小。

    在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在工具栏android中的标题上设置自定义字体

如何在片段中添加菜单?

如何在JFrame上自定义标题栏?

如何在自定义的tinymce下拉菜单中添加工具栏按钮?

ionic:如何向标题栏添加自定义元素或如何为每个视图添加自己的标题栏?

如何在Android中添加自定义选项(Dowload Image)和其他共享选项

如何在rmarkdown的导航栏标题字段中添加自定义徽标?

如何为表单的标题栏,系统菜单图标以及最小化,最大化和关闭按钮设置自定义光标?

如何在带电子的macOS的菜单栏中添加自定义菜单图标?

如何在标题栏中添加图标

如何在Kivy中创建自定义标题栏?

如何在导航栏中添加菜单?

向MetroWindow标题栏中的图标添加自定义样式

如何在React Native中向标题添加自定义标题和按钮?

如何在Nautilus的右键菜单中添加“使用自定义命令打开”选项

在自定义标题栏上设置标题文本

如何在openFrameworks中自定义OS X菜单栏选项?

适用于Linux的Chrome自定义标题栏选项

自定义标题栏不在Android中的TabHost上方

Android:使用“自定义ListAdapter”视图在“自定义标题栏”中未显示标题

自定义图片作为标题栏中的图标

如何在Java中创建标题栏菜单

NSWindow标题栏自定义

Android标题栏自定义视图不拉伸

如何在Android中动态添加菜单项

如何在Android中创建自定义Spinner工具栏下拉菜单

在标题栏工具栏android中添加菜单项文本和文本

使用 Prism 和自定义标题栏时如何在 MasterDetail 页面中显示菜单

如何在 Woocommerce 我的帐户侧边栏中添加新的自定义标题?