滑动抽屉出现在所有活动中
我正在开发一个包含许多活动的应用程序 我用滑动抽屉创建了自己的菜单(我不想使用内置菜单按钮) 由于滑动抽屉位于屏幕底部并包含我的菜单按钮,
我需要的是使滑动抽屉出现在我的所有活动中,
我尝试创建一个活动并将其内容视图设置为包含抽屉的 xml 文件然后将该活动扩展到所有其他活动,但此解决方案不起作用,
所以有什么建议吗?
I am developing an application that contains many activities
and i created my own menu (i don't want to use the built in menu button) with the Sliding Drawer
as the sliding drawer is at the bottom of the screen and contains my menu buttons
what i need is to make that sliding drawer to appear in all my activities
i tried to create an activity and set it's content view to the xml file that includes the drawer and then extends that activity in all other activities but this solution doesn't work
so any suggestions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展才是正确的方法。只需以正确的方式重写 setContentView 即可。
这是工作示例,但我不是使用抽屉,而是使用创建的自定义选项卡栏:
使用抽屉定义布局,如下所示:
这是
act_layout.xml
这将是包含所有其他布局的基本布局在 act_content 框架中。
接下来,创建一个基础活动类,并执行以下操作:
我们所做的基本上是拦截对 setContentView(int resId) 的所有调用,从 xml 膨胀我们的抽屉布局,膨胀我们的活动布局(通过方法调用中提供的 reId) ,根据需要组合它们,并设置为activity的contentView。
编辑:
创建完上面的内容后,只需像往常一样继续编写应用程序,创建布局(不提及抽屉)创建活动,但不要扩展简单的活动,而是扩展 DrawerActivity,如下所示:
会发生什么,setContentView (R.layout.some_layout) 被拦截。您的 DrawerActivity 加载您从 xml 提供的布局,加载抽屉的标准布局,组合它们,然后将其设置为活动的 contentView。
Extending is the right way. Just override setContentView in the right way.
Here's the working example, but instead of drawer, I use a created a custom tabbar:
Define a layout with your drawer like this:
this is
act_layout.xml
This will be your base layout to contain all other layouts in the act_content frame.
Next, create a base activity class, and do the following:
What we do, is basically intercept all calls to setContentView(int resId), inflate our layout for drawer from xml, inflate our layout for activity (by reId provided in method call), combine them as we need, and set as the contentView of the activity.
EDIT:
After you've created the stuff above, just proceed to write an app as usual, create layouts (without any mention of a drawer) create activities, but instead of extending simple activity, extend DrawerActivity, like so:
What happens, is that setContentView(R.layout.some_layout) is intercepted. Your DrawerActivity loads the layout you provided from xml, loads a standart layout for your drawer, combines them and then sets it as contentView for the activity.
三年后,对于那些可能没有完全听从奥尔洛夫先生的回答的人来说,这是这个重要问题的完整解决方案。
他制作层次视图的方法完全没问题,但有一些小错误可能会误导初学者开发人员。
所以100%工作的解决方案是这样的:
activity_drawer.xml
DrawerActivity.java
MainActivity.java
不要忘记在清单中声明DrawerActivity。
希望有帮助。
Finally after 3 years, here's the complete solution to this important question for whom may have not been completely guided by Mr. Orlov's answer.
His method for making a hierarchy view was completely OK but there was some small mistakes that may mislead beginner developers.
So the 100% working soloution would be like this :
activity_drawer.xml
DrawerActivity.java
MainActivity.java
Don't forget to declare DrawerActivity in manifest.
Hope it helped.
我知道这已经晚了两年,但对于那些想在这里得到答案的人来说,这已经晚了。奥尔洛夫先生做出了适当的回应,只需要进行一些调整。
另外:如果您希望滑动抽屉出现在布局的顶部(废话!),请将位于滑动抽屉之后的frameLayout 放置在滑动抽屉之前。
I know this is two years late but for those who would like an answer here it is. Mr Orlov had an appropriate response, just a few adjustments needed.
ALSO: If you want your sliding drawer to appear on top of your layout (duh!) then place the frameLayout that is after the slidingDrawer before the slidingDrawer.