Java Android 以编程方式包含布局(即没有 XML)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3195668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android programmatically include layout (i.e. without XML)
提问by Jon Willis
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 中明确使用包含语句每个 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 代码的更好方法(即使标题栏本身需要在活动之间略有不同。)
采纳答案by Yoni Samlan
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
然后我会定义一个抽象getContentAreaLayoutId()
方法,CustomTitlebarActivity
它返回ID
每个子类标题栏下方的内容布局;底座onCreate()
的CustomTitlebarActivity
,然后只需调用
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 herecase).
或者,您可以让用于获取内容区域的抽象方法返回 aView
而不是 an int
,从而为您提供更大的灵活性来动态构建您的视图(但迫使您在简单的将这个 XML 布局转储到这里的情况下自己膨胀它们)。
回答by acoustic
I met this issue too, and I just solved it now. I think my solution is easier:
我也遇到了这个问题,现在才解决。我认为我的解决方案更简单:
create a 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);
创建一个充气机:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
膨胀子布局:
View childLayout = inflater.inflate(R.layout.child, (ViewGroup) findViewById(R.id.child_id));
将其添加到父级:
parentLayout.addView(childLayout);
It's done, enjoy it!
大功告成,欣赏!