2013-04-01 114 views
0

我正在嘗試使用LayoutInflater在另一個xml佈局中添加一個xml佈局。 但它沒有顯示。在Android中使用LayoutInflater在另一個xml佈局中未顯示xml佈局

請檢查我的源代碼。

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.webkit.WebView; 
import android.widget.RelativeLayout; 

public class CustomeWebView extends Activity { 
RelativeLayout relLay; 
WebView webview; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_new); 
    relLay = (RelativeLayout) findViewById(R.id.main_relLay); 
    LayoutInflater inflater = (LayoutInflater) getApplication() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v_child = inflater.inflate(R.layout.row, null); 
    relLay.addView(v_child); 
} 
} 

主要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" > 

<ScrollView 
    android:id="@+id/scrollview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <RelativeLayout 
     android:id="@+id/main_relLay" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#0B7A3B" > 
    </RelativeLayout> 
</ScrollView> 

</RelativeLayout> 

孩子xml文件

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

<RelativeLayout 
    android:id="@+id/row_relLay" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#F70925"> 

    <WebView 
     android:id="@+id/row_webView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:visibility="gone"> 
    </WebView> 
</RelativeLayout> 

</LinearLayout> 
+0

將webview可見性更改爲可見。 –

+0

好的,我會試試.... –

+0

什麼都沒有......它顯示空白的白屏...... –

回答

0

android:fillViewport="true"添加到您的滾動型。它應該做的伎倆。

設置爲true時,此屬性會導致滾動視圖的子級擴展到ScrollView的高度(如果需要)。當孩子比ScrollView高時,該屬性不起作用。

注:

  1. 您應該刪除您周圍的滾動型中的RelativeLayout main_new.xml
  2. 你應該刪除你的周圍在RelativeLayout的row.xml

的LinearLayout中兩者都是不必要的