android - 如何在 Android 中的操作栏上方显示自定义 View

标签 android android-layout

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

enter image description here

我有两个问题:

1.这样的元素是如何被调用的以及我如何实现这样的东西?

2.它们怎么能在操作栏上方?

最佳答案

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

// 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);

您不需要任何特殊的方式将其放在顶部!只要按照你喜欢的方式设计它,它就会在顶部!

关于android - 如何在 Android 中的操作栏上方显示自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982681/

相关文章:

java - 将微调项目存储在字符串值中

android - 底部没有 TitleBar 的 ActionBar

android - BroadcastReceiver Intent.ACTION_PACKAGE_ADDED/REMOVED Android Oreo

java - 在 Android 中更改 ImageView 的位置时出现问题

android - 在缺口设备模拟器上测试应用程序

android - 在 Android 中,是否保证 AbstractWindowedCursor.isBlob() 是正确的?

android - 为什么 TextView 只在 Galaxy Nexus 中显得更大?

android - 当 Android View 在应用程序中可见时是否会触发任何事件?

c# - Xamarin.Android SQLite SQLiteConnection.Insert 不返回插入对象的 ID

android - 在 Kotlin 中传递多个 lambda 函数以处理成功/失败的最佳方法