繁体   English   中英

android中使用LayoutInflater在另一个xml布局中未显示xml布局

[英]xml layout not showing in another xml layout using LayoutInflater in 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>

android:fillViewport="true"行添加到ScrollView中。 它应该可以解决问题。

设置为true时,此属性将导致滚动视图的子级在需要时扩展到ScrollView的高度。 当子对象比ScrollView高时,该属性无效。

注意 :

  1. 您应该在main_new.xml中删除ScrollView周围的RelativeLayout
  2. 您应该在row.xml中删除RelativeLayout周围的LinearLayout

两者都是不必要的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM