避免为具有相同布局的活动重写代码
我想在我的应用程序上运行多个活动;我希望每个活动都有一个线性布局并显示图像作为标题;基本上我希望每个布局都像这样开始:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Grey"
>
<ImageView android:id="@+id/imageHeader" android:src="@drawable/tf_header" android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="@color/Black" android:scaleType="fitXY"></ImageView>
是否可以不对每个布局重复此代码?我可以使用主题或样式来避免它吗?谢谢您的回复。
I'd like to run several Activities on my Application;I'd like each Activity to have a Linear Layout and to show an image as heading; basically I'd like every layout to start like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Grey"
>
<ImageView android:id="@+id/imageHeader" android:src="@drawable/tf_header" android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="@color/Black" android:scaleType="fitXY"></ImageView>
Is it possible not to reapeat this code for each layout? Could I use themes or styles to avoid it? Thank you for your replies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,可以使用
标签,如 布局技巧。对于您的示例,我会将图像添加为
并在每个 Activity 布局 xml 中都有一个LinearLayout
Yes thats possible use the
<include>
tag as described in Layout Tricks.For your example I would add the image as
<include>
and have aLinearLayout
in each Activity layout xml我的一个应用程序对 30 个左右的类使用相同的 xml 文件,我只是在代码中修改它来自定义它。这种方法可能有效,只需将 xml 中的每个标签留空并将其设置在 .class 中即可
One of my apps uses the same xml file for 30 or so classes, i just modify it in code to customize it. That approach may work, just leave every label in the xml blank and set it in the .class