Android:创建自定义容器视图

时间:2010-12-20 11:50:49

标签: java android layout view

我正在尝试在android中创建一个自定义视图(或更好的布局),用作2个子视图的容器(可以将其视为一个垂直分隔2个容器的条,可以上下滑动)。

我想像xml中的布局一样使用它,以便您可以在其中嵌套任何视图。 我想到了类似的东西:     

            <view class="at.calista.quatscha.views.SwipebarLayout"
            android:id="@+id/sbl"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <!-- Top View -->
            <Button android:text="Top" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

            <!-- Bottom View -->
            <Button android:text="Bottom" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</view>

这个xml应该给我两个按钮之间的可滑动条。

但是,我不知道如何做到这一点,我正在扩展LinearLayout atm,但我如何告诉我的孩子如何定位自己?

1 个答案:

答案 0 :(得分:1)

而不是

<view class="at.calista.quatscha.views.SwipebarLayout"
        android:id="@+id/sbl"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

你可以使用

<at.calista.quatscha.views.SwipebarLayout
        android:id="@+id/sbl"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

如果您的SwipebarLayout类扩展了LinearLayout,它的行为方式与您在代码中使用LinearLayout标记的方式完全相同,您可以使用与LinearLayout完全相同的方式定位子项。当然,如果你已经从LinearLayout覆盖了一些方法,那么功能就不同于那个部分了。