2013-04-01 113 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中两者都是不必要的