Android: Creating a custom container-view

2019-07-03 16:01发布

I'm trying to create a custom view (or better, layout) in android, that serves as a container for 2 child views (Think of it as a bar that separates 2 containers vertically, that can be swiped up and down).

I'd like to use it like a layout in xml, so that you can nest any views in it. I thought of something like that:

            <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>

This xml should give me the swipeable bar between the two buttons.

However, I don't know how to accomplish this, I'm extending LinearLayout atm, but how do I tell my children how to position themselves?

1条回答
男人必须洒脱
2楼-- · 2019-07-03 16:50

instead of

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

you can use

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

If your SwipebarLayout class extends LinearLayout it will behave exactly same way as if you would have LinearLayout tag in your code instead and you can position the children exactly same way you would with LinearLayout. Of course if you have overridden some methods from LinearLayout then the functionality is different from that part.

查看更多
登录 后发表回答