【问题标题】:Action Bar Background size, Android操作栏背景大小,Android
【发布时间】:2015-06-02 21:34:38
【问题描述】:

我想将操作栏背景颜色更改为自定义颜色。这是我目前得到的:

如何在整个操作栏宽度上获得这种颜色?

我的操作栏xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#0c555c"
    android:gravity="center"
    android:id="@+id/action_bar_title"
    android:paddingEnd="0dp"
    android:paddingStart="0dp"
    android:showAsAction="ifRoom">
</RelativeLayout>

onCreate方法:

RelativeLayout actionBar = (RelativeLayout)findViewById(R.id.action_bar_title);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);

【问题讨论】:

    标签: java android android-actionbar


    【解决方案1】:

    如果您正在设计您的自定义 actionBar,而不是像这样编写代码:

      ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8B58A3")));
    
        LayoutInflater inflater = LayoutInflater.from(this);
        @SuppressLint("InflateParams") 
        View v = inflater.inflate(R.layout.titleview, null);
    
       actionBar.setCustomView(v);
    

    希望对你有帮助!

    【讨论】:

    • @SuppressLint("InflateParams") 是什么意思?感谢它的工作!
    • Ernis 这是由于 null 的存在而被传递的,这意味着“当膨胀布局时,避免将 null 作为父视图传递,否则膨胀布局根部上的任何布局参数都将被忽略。”所以要忽略这个警告,据我所知,我们通过@suppressLint
    • 感谢您的解释!
    【解决方案2】:

    请在最后加上这一行。

    工具栏父级 =(Toolbar) actionBar.getParent(); parent.setContentInsetsAbsolute(0,0);

    【讨论】:

    • 什么是视图?因为我没有使用那个
    • 在你这边是actionBar,你在里面放了布局文件。
    • mhm .. 我做错了,因为当我最后输入该代码时,我得到一个 java.lang.NullPointerException 的新错误
    猜你喜欢
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    相关资源
    最近更新 更多