自定义操作栏未显示

时间:2014-12-13 14:06:32

标签: android android-actionbar

我的活动是给自定义操作栏充气,以便在上下移动时将渐变从不透明的黑色变为透明。

编辑: - 我更改了我的代码,现在我得到了这个输出: - enter image description here

我的代码: -

  public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
                //action bar setting
                //Getting Action Bar
                ActionBar mActionBar= getActionBar();

                LayoutInflater minflate =(LayoutInflater) mActionBar.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View mview = minflate.inflate(R.layout.subgrid_ab,null);

                mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE);
                mActionBar.setCustomView(mview, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

                TextView mtext =(TextView)mview.findViewById(R.id.subgridab_text);
                  mtext.setText("General");
                setContentView(R.layout.gridsub);
}

这意味着视图已膨胀,但渐变效果仍未显示。

subgrid_ab.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="wrap_content"
    android:background="@drawable/custom_ab"
     >

    <TextView 
        android:id="@+id/subgridab_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:text="dd"
        android:textSize="24sp"
        android:layout_centerInParent="true"/>



</RelativeLayout>

custom_ab.xml: -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >



    <gradient
        android:startColor="#FF000000"
        android:endColor="#00000000" 
        android:angle="270"
        />

</shape>

那么我做错了什么?

1 个答案:

答案 0 :(得分:0)

LayoutInflater minflate = LayoutInflater.from(this);
View mview = minflate.inflate(R.layout.subgrid_ab,null);
mActionBar.show();

ActionBar如何了解你的mview?
可能会有所帮助:

getActionBar().setCustomView(mview);

<强>更新

@Mohit ok,尝试将“@ + id”设置为Relative布局并将此视图设置为id作为操作栏的自定义视图

View mview = minflate.inflate(R.layout.subgrid_ab,null);
View customView = mview.findViewById(R.id.yourRelativeLayoutId);

mActionBar.setCustomView(customView, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));