避免为具有相同布局的活动重写代码

发布于 2024-12-27 16:54:11 字数 592 浏览 2 评论 0原文

我想在我的应用程序上运行多个活动;我希望每个活动都有一个线性布局并显示图像作为标题;基本上我希望每个布局都像这样开始:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漆黑的白昼 2025-01-03 16:54:11

是的,可以使用 标签,如 布局技巧

对于您的示例,我会将图像添加为 并在每个 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 a LinearLayout in each Activity layout xml

懷念過去 2025-01-03 16:54:11

我的一个应用程序对 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文