如何在FrameLayout中显示/隐藏ImageView

问题描述:

我试图显示/隐藏一个具体的ImageView,它在FrameLayout内。同时,我也会隐藏或显示(与此相反,以ImageView),该LinearLayout与ID recents_linear_layout如何在FrameLayout中显示/隐藏ImageView

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

在此先感谢。

+0

的ImageView的是你必须把它传递在某种程度上 – Blundell 2012-04-22 19:25:13

+0

是的,那是你RecentsHorizo​​ntalScrollView的范围之内我需要的。但我不知道如何检索ID,使用findViewById方法 编辑:似乎我修好了,编辑主帖 – user809486 2012-04-22 19:27:59

+0

是的,你把它移到范围。你不应该编辑它,只需在下面回答你自己的问题。 – Blundell 2012-04-22 19:35:43

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

固定 修复程序使旧的布局和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>