簡體   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