10

コンテナのレイアウトに影響を与えないように、Androidの他の要素の上に要素をフロートさせることは可能ですか?

私が達成したい効果は、浮かぶ吹き出しのような要素です。

ここに画像の説明を入力してください

上の黄色いボックスは、他の要素の上に浮かせたい要素ですが、ご覧のとおり、コンテナを押し下げて、下の灰色の領域の上に浮かぶのではなく、緑のバーの下に黒いスペースを作成します。

RelativeLayoutは要素を適切に配置できますが、配置された要素がそのコンテナーの境界を拡張しないようにする方法を見つけることができません。これはAndroidでも可能ですか?


アナロジー

  • Webの世界では、同等の要素は「position:absolute」を持つ要素であるため、「フロー」(つまり:layout)の一部としてカウントされません。
  • WPFの世界では、同等のものはCanvas内に配置される要素になります。Canvasには、レイアウト内のスペースを占める独自の境界がありますが、その子は境界の外側にあり、レイアウトに影響を与えることはありません。
  • iOSの世界では、すべてのビューがキャンバスとして配置されるため(レイアウトマネージャーがないため)、この動作は、要素フレームのxプロパティとyプロパティをオフセットするだけで実現されます。
4

2 に答える 2

14

RelativeLayoutは要素を適切に配置できますが、配置された要素がそのコンテナーの境界を拡張しないようにする方法を見つけることができません。

次に、間違ったコンテナがあります。

例えば:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/big"
        android:textSize="120dip"
        android:textStyle="bold"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/small"/>

</RelativeLayout>

フローティングボタン

ここでは、小さなボタンが大きなボタンの上に浮かんでいます。コンテナがコンテンツビュー全体であるという理由だけで、小さなボタンは「コンテナの境界を拡張」しません。

RelativeLayout(またはFrameLayout)コンテナを、子供が浮かぶ可能性のある領域を含むコンテナとして設定します。それが画面全体であるか、単にサブセットであるかはあなた次第です。逆に、お子様がコンテナの境界に影響を与えている場合は、間違ったコンテナを使用しているか、この役割専用のコンテナを設定する必要があります。

別の例では、Roman Nurikが、Gmailに似たフローティング「元に戻る」を実装する方法を示しています。

フローティングアンドゥバー

于 2012-09-25T23:03:15.147 に答える
2

androidの相対レイアウトを見てください。たとえば、相対レイアウト内のすべてのビューで位置を指定しない場合、それらは互いに重なり合って浮きます。

http://developer.android.com/guide/topics/ui/layout/relative.html

例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#444444"
        android:text="Update"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/lost_of_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/some_button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="text view with lots of text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/some_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Some Button" />

</RelativeLayout>

編集:CommonsWareの方が速かった;)彼の美しい答えを見てください!

于 2012-09-25T23:05:17.673 に答える