2017-01-16 12 views
0

このサンプルアンドロイドXMLレイアウトのトップレベルのレイアウトを取得したいと考えています。私が参照しているトップレベルは、アクティビティ全体を一緒に保持しているViewGroupです。Android:アクティビティの最上位の親レイアウトを取得する方法

私は

getWindow().getDecorView().findViewById(android.R.id.content); 

の認識しています。しかし、私はDecorViewを必要としません。

以下のXMLを参照してください。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/linearLayout"> 
     <WebView 
     android:id="@+id/my_webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
    </LinearLayout> 


</RelativeLayout> 

私はRelativeLayout(私は私の条項に間違っている場合は、私が参照してるトップレベルのレイアウトで、私を修正してください)を取得する必要があります。私は以下のコードを使ってそれを得ることができます。しかし、レイアウトはのgetParent()を使用せずに、トップレベルのレイアウトを取得することができますどのように

<Layout> 
    <Layout> 
     <Layout> 
      <Webview/> 
     <Layout> 
    <Layout> 
<Layout> 

のような深い何をネストされている場合

WebView webView = (WebView) findViewById(R.id.my_webview); 
webview.getParent().getParent() 

。ありがとう!あなたの活動のXML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/parentLayout"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/linearLayout"> 
     <WebView 
     android:id="@+id/my_webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
    </LinearLayout> 


</RelativeLayout> 

+1

あなたの.XMLのparent_layoutにidを与え、あなたのMainActivityでこのIDを定義/キャストし、このIDを必要に応じて使用します。 –

+0

私はこれを、別のアプリケーションで使用できるライブラリで呼びます。今、idのものはこれには当てはまりません。 p.s:IDは、ライブラリを使用するすべてのアプリで異なる場合があります。私が持つことができる唯一の参照はwebviewオブジェクトです。 – Aaron

+0

このコードを作成しましたが、https://hastebin.com/ijiyusicul.csはテストしていません。 – Enzokie

答えて

1

RelativeLayout parentLayout=(RelativeLayout)findViewById(R.id.parentLayout); 

または

getWindow().getDecorView().getRootView(); 

または

getWindow().getDecorView().findViewById(android.R.id.content); 

または

+0

私はこれを、別のアプリで使用できるライブラリで呼びます。今、idのものはこれには当てはまりません。 p.s:IDはライブラリを使用するすべてのアプリで異なる可能性があります。私が持つことができる唯一の参照はwebviewオブジェクトです。 – Aaron

+0

getDecorView()が不要な場合はビューグループを試すことができます。 –

関連する問題