So I've created an Activity subclass called CustomTitlebarActivity. Essentially, each main activity in my app will have a custom titlebar with many common features such as a Home button, a title, a search button, etc. In my current implementation, I am still explicitly using an include statement in the layout XML for each CustomTitlebarActivity:
所以我创建了一个名为CustomTitlebarActivity的Activity子类。基本上,我的应用程序中的每个主要活动都将有一个自定义标题栏,其中包含许多常用功能,如主页按钮,标题,搜索按钮等。在我当前的实现中,我仍然在布局XML中明确使用include语句每个CustomTitlebarActivity:
<include layout="@layout/titlebar" />
It seems natural that I should be able to do this within CustomTitlebarActivity. I have two questions: What code can replace this include tag, and where should I put the code? (My first instinct would be to put it in CustomTitlebarActivity's setContentView method.)
我应该能够在CustomTitlebarActivity中执行此操作,这似乎很自然。我有两个问题:什么代码可以替换这个包含标记,我应该在哪里放置代码? (我的第一直觉是将它放在CustomTitlebarActivity的setContentView方法中。)
On a related note, I would appreciate insight into better ways to reuse android UI code (even if, per se, the titlebars need to vary slightly between activities.)
在相关的说明中,我希望能够深入了解重用Android UI代码的更好方法(即使本身标题栏需要在活动之间略有不同。)
2 个解决方案
#1
15
Personally, I'd probably write my Activity
subclass to always setContentView
to a layout file containing a vertical fill_parent
LinearLayout
containing only my title bar:-
就个人而言,我可能会将我的Activity子类编写为总是将setContentView设置为包含垂直fill_parent LinearLayout的布局文件,该文件只包含我的标题栏: -
<LinearLayout android:id="@+id/custom_titlebar_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--titlebar here-->
</LinearLayout>
Then I'd define an abstract getContentAreaLayoutId()
method in CustomTitlebarActivity
that returns the layout ID
of the content below the titlebar for each subclass; the base onCreate()
of CustomTitlebarActivity
would then just call
然后我在CustomTitlebarActivity中定义一个抽象的getContentAreaLayoutId()方法,该方法返回每个子类标题栏下面的内容的布局ID;然后,CustomTitlebarActivity的基础onCreate()将调用
setContentView(R.layout.custom_titlebar_activity_frame_from_above);
View.inflate(this, getContentAreaLayoutId(), findViewById(R.id.custom_titlebar_container));
Alternatively, you could have your abstract method for getting the content area return a View
rather than an int
, giving you more flexibility to construct your views dynamically (but forcing you to inflate them yourself in the simple just dump this XML layout here case).
或者,您可以使用抽象方法来获取内容区域而不是返回一个视图而不是一个int,这样您可以更灵活地动态构建视图(但是在这里简单地转储这个XML布局的情况下,强制您自己给它们充气)。
#2
51
I met this issue too, and I just solved it now. I think my solution is easier:
我也遇到了这个问题,我现在就解决了。我认为我的解决方案更容易:
-
create a inflater:
创建一个inflater:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
-
inflate the child layout:
给孩子布局充气:
View childLayout = inflater.inflate(R.layout.child, (ViewGroup) findViewById(R.id.child_id));
-
add it into parent:
将其添加到父级:
parentLayout.addView(childLayout);
It's done, enjoy it!
它完成了,享受它!
#1
15
Personally, I'd probably write my Activity
subclass to always setContentView
to a layout file containing a vertical fill_parent
LinearLayout
containing only my title bar:-
就个人而言,我可能会将我的Activity子类编写为总是将setContentView设置为包含垂直fill_parent LinearLayout的布局文件,该文件只包含我的标题栏: -
<LinearLayout android:id="@+id/custom_titlebar_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--titlebar here-->
</LinearLayout>
Then I'd define an abstract getContentAreaLayoutId()
method in CustomTitlebarActivity
that returns the layout ID
of the content below the titlebar for each subclass; the base onCreate()
of CustomTitlebarActivity
would then just call
然后我在CustomTitlebarActivity中定义一个抽象的getContentAreaLayoutId()方法,该方法返回每个子类标题栏下面的内容的布局ID;然后,CustomTitlebarActivity的基础onCreate()将调用
setContentView(R.layout.custom_titlebar_activity_frame_from_above);
View.inflate(this, getContentAreaLayoutId(), findViewById(R.id.custom_titlebar_container));
Alternatively, you could have your abstract method for getting the content area return a View
rather than an int
, giving you more flexibility to construct your views dynamically (but forcing you to inflate them yourself in the simple just dump this XML layout here case).
或者,您可以使用抽象方法来获取内容区域而不是返回一个视图而不是一个int,这样您可以更灵活地动态构建视图(但是在这里简单地转储这个XML布局的情况下,强制您自己给它们充气)。
#2
51
I met this issue too, and I just solved it now. I think my solution is easier:
我也遇到了这个问题,我现在就解决了。我认为我的解决方案更容易:
-
create a inflater:
创建一个inflater:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
-
inflate the child layout:
给孩子布局充气:
View childLayout = inflater.inflate(R.layout.child, (ViewGroup) findViewById(R.id.child_id));
-
add it into parent:
将其添加到父级:
parentLayout.addView(childLayout);
It's done, enjoy it!
它完成了,享受它!