如何在Android中的操作栏上方显示自定义视图

时间:2018-12-31 00:55:40

标签: android android-layout

来自Google音乐应用的屏幕截图:

enter image description here

我有以下两个问题

1。。如何调用这样的元素?如何实现这样的内容?

2。。它们如何位于操作栏上方?

1 个答案:

答案 0 :(得分:3)

这是实现此目的的简单方法。 ActionBar将其布局保留在 ActionBarContainer 类中,该类仅继承自 FrameLayout 。因此,为了在ActionBar上显示某些内容,您需要获取对ActionBarContaine r的引用,然后向其中添加您的自定义视图。这是代码

// first get the id of Action bar container    
    int actionbarContainerViewID = getResources().getIdentifier("action_bar_container", "id", "android");     
    FrameLayout actionBarContainer = (FrameLayout)findViewById(actionbarContainerViewID);

// bring the layout inflator to inflate your custom layout      
    LayoutInflater mInflater = getLayoutInflater();        
    View customView = mInflater.inflate(R.layout.yourCustomDesign, null);
    actionBarContainer.addView(customView);

您将不需要任何特殊方法将其放在顶部!只需根据自己的喜好进行设计,它将位于顶部!