我的情况如下。我有一个Activity
包含三个主要部分。在此活动的顶部有一个工具面板,其中包含两个ImageButtons
. 在活动的底部还有类似于顶部的工具面板。
此活动的主要组件位于屏幕中心,LinearLayout
即包含 3x3ImageButtons
网格。为了获得上下文,我快速描述了这些网格按钮的用途。
这个 3x3 网格描述了特定日期的此按钮的状态(比如说切换按钮及其状态为开/关)。所以有一天你有一组 3x3,它们在特定的日子里有自己的状态。
现在我想要实现的是具有允许用户分别在网格或日期之间滚动的功能。我的想法是拥有ViewFlipper
作为活动的主要组件(而不是LinearLayout
)和一个或多个按钮网格。这是我已经拥有的。我可以在另一天将 3x3 切换到另一个 3x3(带状态)。为此,我只需复制/粘贴我的 3x3 网格 XML 定义并将其作为子项以查看翻转器(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>
我向这个 fipper 添加了这个布局的视图(这个代码是通过 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">