2016-02-29 50 views
1

很多ScrollView的例子,但我對RelativeLayout沒有任何意見如何檢查Android中的屏幕是否真的可見?

在RelativeLayout中,有些視圖會覆蓋另一個視圖。 我可以在xml中檢查它,但如何檢查它與Java代碼? 使用isShown(),willNotDraw(),hasWindowFocus(),getLocalVisibleRect(),那些不起作用。

案例1:容器佈局真的是在屏幕上

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!--container layout is really on screen--> 
    <fragment 
     android:id="@+id/map_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 
     <ImageView 
      android:id="@+id/icon" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      /> 
     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="18sp" 
      /> 
     </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

案例2:容器佈局是不是真的在屏幕上

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 
     <ImageView 
      android:id="@+id/icon" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      /> 
     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="18sp" 
      /> 
     </LinearLayout> 
    </LinearLayout> 

    <!--container layout is not really on screen--> 
    <fragment 
     android:id="@+id/map_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 
</RelativeLayout> 

回答

1

嘗試使用此

if (myView.isShown()) { 
     // Its visible 
    } else { 
     // Either gone or invisible 
    } 
+0

在情況2中,容器是可見的。但它不顯示在屏幕上 –

0

變化情況2:容器佈局是不是真的在屏幕上

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

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/icon" 
      android:layout_width="50dp" 
      android:layout_height="50dp" /> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="18sp" /> 
    </LinearLayout> 

    <!--container layout is not really on screen--> 
    <FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <!-- fragment --> 
    </FrameLayout> 

</RelativeLayout> 

MainActivity

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = MainActivity.class.getSimpleName(); 
    private FrameLayout frameLayout; 
    private LinearLayout container; 
    private RelativeLayout parent; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     frameLayout = (FrameLayout) findViewById(R.id.frameLayout); 
     container = (LinearLayout) findViewById(R.id.container); 
     parent = (RelativeLayout) findViewById(R.id.parent); 

     //container.bringToFront(); 
     Log.e(TAG, "container: " + parent.indexOfChild(container)); 
     Log.e(TAG, "frameLayout: " + parent.indexOfChild(frameLayout)); 

     if (parent.indexOfChild(container) == parent.getChildCount() - 1) { 
      Log.e(TAG, "container is visible"); 
     } else { 
      Log.e(TAG, "container is not visible"); 
     } 
    } 
} 

如果容器指數等於孩子在父母算則是另一種觀點的前面,可見。否則容器重疊在其他佈局之後。

我希望這對你有幫助。

+0

謝謝,我知道如何顯示容器,但我想知道如何檢查它。 –

相關問題