【问题标题】:How to inflate/add view behind al other views in RelativeLayout?如何在 RelativeLayout 中的所有其他视图后面膨胀/添加视图?
【发布时间】:2015-09-23 08:05:21
【问题描述】:

我有一个相对布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    tools:context="com.pinder.moffel.pinder.DeleteActivity"
    android:id="@+id/deleteBackground">

    <com.andtinder.view.CardContainer
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tinderview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0"/>


</RelativeLayout>

我正在使用库 AndTinder:https://github.com/kikoso/Swipeable-Cards

我已经知道如何添加新卡片,但问题是,由于正在添加新布局,新卡片会显示在其他视图的顶部

我想在 Activity 加载时添加 3 张卡片,然后在卡片被刷掉时添加一张新卡片所有其他卡片之后。

如何在 RelativeLayout 中的所有其他视图后面扩展新视图?

一些附加代码:

JAVA 代码

SimpleCardStackAdapter adapter = null;
CardContainer mCardContainer = (CardContainer) findViewById(R.id.tinderview);
adapter = new SimpleCardStackAdapter(this);
CardModel newCard = new CardModel(null, null, image); //image is retrieved before
adapter.add(newCard); //the new card will now be on top of the others, i want it to be behind them

mCardContainer.setAdapter(adapter);

正在添加的 XML 视图

<RelativeLayout
    android:id="@+id/global_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_alignWithParentIfMissing="true"
        android:layout_centerHorizontal="true"
        android:scaleType="fitCenter"
        tools:src="@drawable/picture1"/>

    <View
        android:id="@+id/divider_bottom"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_below="@id/image"
        android:background="@color/card_outline" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="10dp"
        android:layout_alignBottom="@+id/layoutButton"
        android:layout_below="@+id/divider_bottom"
        android:background="@color/card_bg"
        />

    <!-- An invisible view aligned to the center of the parent. Allows other
        views to be arranged on either side -->
    <LinearLayout
        android:id="@+id/layoutButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/divider_bottom"
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/image_1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/denied"
            android:padding="10dp"
            android:layout_marginRight="80dp"/>

        <ImageButton
            android:id="@+id/image_2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/heart"
            android:padding="10dp"
            android:layout_alignParentRight="true" />

    </LinearLayout>



</RelativeLayout>

在另一个问题 (How to inflate a view behind another) 中,答案指出不可能在其他人后面添加视图,但我认为必须有一种方法来实现这一点。

【问题讨论】:

    标签: android android-layout android-adapter android-relativelayout


    【解决方案1】:

    添加新视图时,它会自动将其置于顶部。

    您可以使用addView(your_view, 0) 这将在布局的索引 0 上添加视图

    【讨论】:

    • 我猜这在RelativeLayout中不起作用。当我测试它时,它没有任何效果。
    【解决方案2】:

    你可以使用ViewAnimator

    http://developer.android.com/reference/android/widget/ViewAnimator.html 上提供了其文档

    在此您需要在 View Animator 中创建两个子布局。您可以定义自己的子布局。并且可以使用 next 和 previous 方法来处理事件。我之前在我的一个项目中使用过它。

    【讨论】:

    • 我会发布我的代码,这可能对你有帮助。您也可以使用取景器。你可以查看示例应用程序play.google.com/store/apps/… 我用它来显示卡片
    【解决方案3】:

    在您的每个视图上,您​​都有 view.bringToFront() 可以用来将该视图放在所有其他视图的前面。如果没有其他办法,你可以在你的所有其他视图上使用它,在你膨胀你的视图之后。

    【讨论】:

    • 如何选择其他视图,因为它们具有相同的 ID?
    • 你可以迭代你的RelativeLayout的孩子,但我不确定这是一个很好的解决方案......你需要使用relativeLayout.getChildCount()和relativeLayout.childAtIndex(index);
    猜你喜欢
    • 2012-04-05
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多