[英]How can I access the layout in a class extending Activity that is to be subclassed by another Activity?
I'm using a SideNavigationBar (in addition to ActionBarSherlock) in my Android App, and I'm a dilemma about how to go about creating a class extending SherlockActivity. 我在Android应用程序中使用SideNavigationBar(除了ActionBarSherlock之外),而对于如何创建扩展SherlockActivity的类则感到困惑。
Since all my activities have a SideNavigation bar and a similarly themed ABS, it makes sense that I create generic subclass of SherlockActivity that initializes the SideNavigation and ABS that will be constant throughout my Activities, and then extend this subclass for all of my unique Activities (Home, settings, etc.). 由于我所有的活动都有SideNavigation栏和类似主题的ABS,因此我创建SherlockActivity的通用子类是很有意义的,该子类初始化SideNavigation和ABS,这些子类将在我的所有Activity中保持不变,然后将该子类扩展到我所有的唯一Activity(主页,设置等)。 So, for example, HomeActivity extends ActionBarActivity, which extends SherlockActivity. 因此,例如,HomeActivity扩展了ActionBarActivity,后者扩展了SherlockActivity。 ActionBarActivity is where I want all the customizing of the ABS and initializing the Sidebar to occur. 我希望在ActionBarActivity中自定义ABS并初始化补充工具栏。
The problem is that I don't know how to access the layout from my ActionBarActivity, which is necessary since that's where I get the SidenavigationBar (using findViewById). 问题是我不知道如何从ActionBarActivity访问布局,这是必需的,因为这是我获取SidenavigationBar的地方(使用findViewById)。 If I was doing this in HomeActivity, i would access the layout using findViewById after setting the content view (setContentView), but since I never set the content view in ActionBarActivity, i'm not quite sure how I can access the SidenavigationBar from the layout. 如果我在HomeActivity中进行此操作,则在设置内容视图(setContentView)后将使用findViewById访问布局,但是由于我从未在ActionBarActivity中设置内容视图,因此我不确定如何从布局访问SidenavigationBar 。
Thanks for the help! 谢谢您的帮助!
but since I never set the content view in ActionBarActivity, i'm not quite sure how I can access the SidenavigationBar from the layout. 但由于我从未在ActionBarActivity中设置内容视图,因此我不确定如何从布局访问SidenavigationBar。
If I understand your question, HomeActivity calls setContentView()
but you want to access the layout in it's parent ActionBarActivity? 如果我理解您的问题,HomeActivity会调用setContentView()
但您想访问其父级ActionBarActivity中的布局?
Then simply make a new method in ActionBarActivity (perhaps initSideNavigationBar()
). 然后只需在ActionBarActivity中创建一个新方法(也许是initSideNavigationBar()
)。 This new method can call findViewById()
just like before as long as you call it after setContentView()
in HomeActivity. 此新方法可以像之前一样调用findViewById()
,只要您在HomeActivity中的setContentView()
之后调用它即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.