简体   繁体   中英

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. 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

<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 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. 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. When the child is taller than the ScrollView, the attribute has no effect.

Note :

  1. You should remove the RelativeLayout surrounding your ScrollView in main_new.xml
  2. You Should remove the LinearLayout surrounding your RelativeLayout in row.xml

Both are unnecessary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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