如何使 xml 定义可重用(某种模板)?

发布于 2024-11-05 10:37:00 字数 2428 浏览 3 评论 0原文


我的情况如下。我有一个 Activity,其中包含三个主要部分。在此活动的顶部有一个带有两个 ImageButtons 的工具面板。在活动的底部还有与顶部类似的工具面板。

此活动的主要组件位于屏幕中心,即包含 3x3 ImageButtons 网格的 LinearLayout。为了了解上下文,我快速描述了这些网格按钮的用途。

这个 3x3 网格描述了特定日期该按钮的状态(假设切换按钮及其状态为开/关)。因此,有一天,您拥有一组 3x3,并且它们在特定日期有自己的状态。

现在我试图实现的是具有允许用户分别在网格或日期之间滚动的功能。我的想法是将 ViewFlipper 作为活动的主要组件(而不是 LinearLayout)和一个或多个按钮网格。这是我已经拥有的。我可以在另一天将 3x3 切换到另一个 3x3(带状态)。为此,我只需复制/粘贴我的 3x3 网格 XML 定义,并将其作为子项来查看 Flipper(x 次)。

我的想法是对 3x3 有一个定义,并像某种模板一样重复使用它 x 次,这样我就可以说“我想要另一个 3x3 ,今天有这样那样的状态,我希望它添加这个作为ViewFlipper 的下一个子级”。

为此(为了拥有 3x3 的组件),我简单地创建了扩展 LinearLayour 的类,并在其中生成/重用按钮的 3x3 定义。当然,我可以通过这个新类中的java代码来做到这一点,但是有没有办法通过重用xml来做到这一点(这更好地维护和阅读)?

我尝试使用 LayoutInflater ,但这可能不是我想要的。

提前致谢

编辑

这里是带有翻转器的主布局的代码

<ViewFlipper android:id="@+id/view_flip" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#FF0000">
    <!-- Here goes 9x9 cubbie layout programaticaly -->
</ViewFlipper>

对于这个翻转器,我添加了带有此布局的视图(此代码是通过 LayoutInflater 分配的)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="1" android:gravity="center">
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:orientation="vertical">
        <ImageButton android:id="@+id/life_att_1" style="@style/AttributeButton" />
        <TextView android:id="@+id/life_att_text_1" android:text="test"
            style="@style/AttributeText" />
    </LinearLayout>
    ...
</LinearLayout>
...
</LinearLayout>

编辑2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1" android:paddingLeft="16dip">

My situation is following. I have one Activity that contains three main parts. At the top of this activity there is an tools panel with two ImageButtons. At the bottom of the activity there is also tools panel similar to top.

Main component of this activity is at the center of the screen which is LinearLayout that contains 3x3 ImageButtons grid. To get the context I quickly describe purpose of these grid buttons.

This 3x3 grid describes states of this buttons (lets say toggle button and its states are on/off) for particular day. So for one day you have one set of 3x3 and these have its own state for particular day.

Now what I trying to achieve is to have functionality which allows user to scroll between grids or dates respectively. My idea is to have ViewFlipper as the main component of the activity (instead of LinearLayout) and one or more grid of buttons. This is what I already have. I am able to switch 3x3 to another 3x3 (with states) for another day. For this I simply copy/paste my 3x3 grid XML definition and put this as children to view flipper (x-times).

My idea is to have one definition of 3x3 and reuse it x-times like some kind of template so I will be able to say "I want to have another 3x3 with so and so states for this day and I want it to add this as next child to ViewFlipper".

For this purpose (to have component of 3x3) I simply made class which extends LinearLayour and within it I want to generate/reuse 3x3 definition for buttons. Sure I am able to do it by java code in this new class but is some way to do it with reusing of xml (which is better to maintain and to read)?

I tried to use LayoutInflater but this is probably not what I am looking for.

Thanks in advance

Edit

Here is code for main layout with flipper

<ViewFlipper android:id="@+id/view_flip" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#FF0000">
    <!-- Here goes 9x9 cubbie layout programaticaly -->
</ViewFlipper>

To this fipper I adding view with this layout (this code is assigned via LayoutInflater).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="1" android:gravity="center">
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:orientation="vertical">
        <ImageButton android:id="@+id/life_att_1" style="@style/AttributeButton" />
        <TextView android:id="@+id/life_att_text_1" android:text="test"
            style="@style/AttributeText" />
    </LinearLayout>
    ...
</LinearLayout>
...
</LinearLayout>

EDIT 2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1" android:paddingLeft="16dip">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

故笙诉离歌 2024-11-12 10:37:00

我在这里找到的唯一方法是将 paddingLeft 属性添加到包装器 LinearLayout (请参阅编辑 2)。我认为这不是正确的解决方案,而是我目前找到的唯一解决方案。

Only way I found here is to add paddingLeft attribute to wrapper LinearLayout (please refer to EDIT 2). I do not think that this is right solution but only solution I currently found.

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