简体   繁体   English

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

[英]xml layout not showing in another xml layout using LayoutInflater in Android

I am trying to add one xml layout in another xml layout using LayoutInflater. 我正在尝试使用LayoutInflater在另一个xml布局中添加一个xml布局。 but its not showing. 但没有显示。

Please check my source code. 请检查我的源代码。

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);
}
}

main xml file 主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>

child xml file 子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>

Add the line android:fillViewport="true" to your ScrollView. android:fillViewport="true"行添加到ScrollView中。 It should do the trick. 它应该可以解决问题。

When set to true, this attribute causes the scroll view's child to expand to the height of the ScrollView if needed. 设置为true时,此属性将导致滚动视图的子级在需要时扩展到ScrollView的高度。 When the child is taller than the ScrollView, the attribute has no effect. 当子对象比ScrollView高时,该属性无效。

Note : 注意 :

  1. You should remove the RelativeLayout surrounding your ScrollView in main_new.xml 您应该在main_new.xml中删除ScrollView周围的RelativeLayout
  2. You Should remove the LinearLayout surrounding your RelativeLayout in row.xml 您应该在row.xml中删除RelativeLayout周围的LinearLayout

Both are unnecessary 两者都是不必要的

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

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