【问题标题】:How to show / hide ImageView in FrameLayout如何在 FrameLayout 中显示/隐藏 ImageView
【发布时间】:2012-04-22 19:20:02
【问题描述】:

我试图显示/隐藏一个特定的ImageView,它位于FrameLayout 内。同时,我要隐藏或显示(与ImageView相反),id为recents_linear_layout

的LinearLayout
<com.android.systemui.recent.RecentsPanelView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recents_root"
android:layout_height="match_parent"
android:layout_width="match_parent">

<FrameLayout
    android:id="@+id/recents_bg_protect"
    android:background="@drawable/status_bar_recents_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:clipToPadding="false"
    android:clipChildren="false">

    <com.android.systemui.recent.RecentsHorizontalScrollView android:id="@+id/recents_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="@dimen/status_bar_recents_right_glow_margin"
        android:divider="@null"
        android:stackFromBottom="true"
        android:fadingEdge="horizontal"
        android:scrollbars="none"
        android:fadingEdgeLength="@dimen/status_bar_recents_fading_edge_length"
        android:layout_gravity="bottom|left"
        android:orientation="horizontal"
        android:clipToPadding="false"
        android:clipChildren="false">

        <LinearLayout android:id="@+id/recents_linear_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:clipToPadding="false"
            android:clipChildren="false">
        </LinearLayout>


    </com.android.systemui.recent.RecentsHorizontalScrollView>

**<ImageView android:id="@+id/landscape"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="0.0dip"
    android:src="@drawable/landscape_img"
    android:visibility="gone" />**

</FrameLayout>

<include layout="@layout/status_bar_no_recent_apps"
    android:id="@+id/recents_no_apps"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible" />

</com.android.systemui.recent.RecentsPanelView>

我的 java 代码抛出 NullPointerException,因为它找不到 ImageView(因为这是在 com.android.systemui.recent.RecentsHorizontalScrollView 上执行的。这是代码

    mLinearLayout = (LinearLayout) findViewById(R.id.recents_linear_layout);
    mLinearLayout.setVisibility(mRecent ? LinearLayout.GONE : LinearLayout.VISIBLE);
mLandScape = (ImageView) findViewById(R.id.landscape);
mLandScape.setVisibility(mRecent ? View.VISIBLE : View.GONE);

提前致谢。

【问题讨论】:

  • ImageView 超出了您的RecentsHorizo​​ntalScrollView 的范围,您必须以某种方式传递它
  • 是的,这就是我需要的。但我不知道如何使用 findViewById 方法检索 ID 编辑:好像我修复了它,编辑了主帖
  • 是的,您已将其移入范围。您不应该真正编辑它,只需在下面回答您自己的问题。
  • 我会回答,但由于声誉,我需要 8 小时 :D

标签: java android imageview android-linearlayout android-framelayout


【解决方案1】:

你可以使用 ((Activity)getContext()).findViewById(R.id.landscape) 虽然你应该避免它,因为不能保证上下文是一个活动。我不明白你为什么要在这个视图中隐藏一些视图。我认为这是活动的责任。您应该使用回调方法创建一些接口来通知活动有关您的事件,然后让它隐藏图像视图。

【讨论】:

    【解决方案2】:

    已修复 修复正在使用旧的 Layout 和 ImageView 作为子项制作一个外部布局容器

        <com.android.systemui.recent.RecentsHorizontalScrollView android:id="@+id/recents_container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="@dimen/status_bar_recents_right_glow_margin"
            android:divider="@null"
            android:stackFromBottom="true"
            android:fadingEdge="horizontal"
            android:scrollbars="none"
            android:fadingEdgeLength="@dimen/status_bar_recents_fading_edge_length"
            android:layout_gravity="bottom|left"
            android:orientation="horizontal"
            android:clipToPadding="false"
            android:clipChildren="false">
    
            <LinearLayout android:id="@+id/child_container"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:clipToPadding="false"
                android:clipChildren="false">
    
                <LinearLayout android:id="@+id/recents_linear_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:clipToPadding="false"
                    android:clipChildren="false" />
    
            <ImageView android:id="@+id/sense_landscape"
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="0.0dip"
            android:src="@drawable/sense_landscape"
            android:visibility="visible" />
    
    
    
            </LinearLayout>
    
    
        </com.android.systemui.recent.RecentsHorizontalScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多