简体   繁体   English

如何在扩展Activity的类中访问布局,该类将由另一个Activity子类化?

[英]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.

相关问题 如何在不扩展活动的类中访问android中的振动器? - How to access vibrator in android in a class that is not extending activity? 另一活动的访问布局 - Access Layout of another activity 如何从Android的Activity类中引用另一个项目的layout / xml文件? - How can I refer to layout/xml files of another project from my Activity class in Android? 如何在父级活动中访问片段布局? - How can i access fragment layout in parent activity? 如何在Android应用程序中全局访问另一个班级的活动? - How can I access an Activity from another class globally in an Android app? 如何将意图从活动传递给扩展LinearLayout的类 - How do I pass an intent from an activity to a class extending LinearLayout 如何从活动中访问方法并将其用于Android中的另一个活动? - How can I access a method from an activity and use it into another activity in Android? 如何在其他活动中执行课程 - How do I execute a class in another activity 如何在活动中加载插页式广告并在其他活动中显示? - How can i load interstitial ad in activity and show is in another activity? 如何使用按钮将主要活动连接到另一个活动? - How can I connect the main activity to another activity using a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM