【问题标题】:Android ViewFlipper - Prevent allocating all layouts inside ViewFlipperAndroid ViewFlipper - 防止在 ViewFlipper 内分配所有布局
【发布时间】:2014-10-11 04:51:45
【问题描述】:

我的活动布局中有一个带有很多 *.xml 布局项(准确地说是 108 个)的 ViewFlipper,问题是当我启动应用程序时,ViewFlipper 中的所有布局都已分配。

有没有办法防止在启动时分配所有 ViewFlipper 项目,并使其仅在加载 viewFlipper 项目时分配?

我的 main_activity.xml 代码

<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:background="#ffececec"
    tools:context=".MyActivity">

    <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <include android:id="@+id/pageMainWin"  layout="@layout/main_win" />
        // Other ViewFlipper items

    </ViewFlipper>

</RelativeLayout>

【问题讨论】:

  • 为什么会有108个布局项?
  • 您基本上可以在不使用时将主布局项设置为layout.setVisiblity(View.GONE),然后在您确实需要时再次提供View.VISIBLE。在需要之前,这不会消耗资源。例如当您向左或向右平移时。

标签: android android-layout viewflipper


【解决方案1】:

尝试在布局中使用 viewStub xml 标签:

<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

     <ViewStub
            android:inflatedId="@+id/pageMainWin"
            android:layout="@layout/main_win"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</ViewFlipper>

然后在您的代码中,以编程方式扩充额外的 xml 布局资源

ViewStub stub = (ViewStub) findViewById(R.id.pageMainWin);
View inflated = stub.inflate();

这里有更多关于viewstub的信息:http://developer.android.com/reference/android/view/ViewStub.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多