我试着以编程方式插入的WebView到我的code [英] Im trying to programmatically insert a WebView into my Code

查看:246
本文介绍了我试着以编程方式插入的WebView到我的code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到它的点击听者surfaceview。

I have a surfaceview with a click listener attached to it.

当我点击它,它缩小到一半的宽度和高度的一半......,并通过其放置在右下角:

When i click it, it shrinks it to half the width and half the Height... and places it in the Lower Right Corner via:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(  
    getWindowManager().getDefaultDisplay().getWidth()/2,
    getWindowManager().getDefaultDisplay().getHeight()/2    
);
rlp.setMargins(
  getWindowManager().getDefaultDisplay().getWidth()/2,    
  getWindowManager().getDefaultDisplay().getHeight()/2, 
  0, 
  0);

它的工作原理为目的,但我想将它背后一个动态web视图。

It works as intended, but i want to place behind it Dynamically a webView.

我想:

WebView wv = makeWebView();
//makeWebView(); is a method of my mainActivity, which simple does.
private Webview makeWebView(){ return new WebView(this); }

我似乎无法得到它背后一切虽然。难道我做错了什么?

I cant seem to get it to be behind everything else though. Am i doing something wrong?

我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RR1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/LL1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

</RelativeLayout>

我MainActivity:

my MainActivity:

package com.example.testfortim;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //SurfaceView sv = new SurfaceView();
    SurfaceView sv = (SurfaceView)findViewById(R.id.surfaceView1);
    sv.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LinearLayout ll = (LinearLayout)findViewById(R.id.LL1);
            //LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)ll.getLayoutParams();
            //RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ll.getLayoutParams().width/2,ll.getLayoutParams().height/2);//(RelativeLayout.LayoutParams)ll.getLayoutParams();
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    getWindowManager().getDefaultDisplay().getWidth()/2, 
                    getWindowManager().getDefaultDisplay().getHeight()/2
                    );
            rlp.setMargins(getWindowManager().getDefaultDisplay().getWidth()/2, getWindowManager().getDefaultDisplay().getHeight()/2, 0, 0);
            ll.setLayoutParams(rlp);

            WebView wv = makeWebView();
            //wv.setBackgroundColor(0);
            //wv.setBackgroundResource(R.color.Blue);
            //wv.setBackgroundResource(Color.BLUE);
            wv.loadData("<html><body>TEST</body></html>", "text/html", "utf-8");
            RelativeLayout rl = (RelativeLayout)findViewById(R.id.RR1);
            //rl.addView(wv);

            //ll.addView(wv);


        }

    });

}

protected WebView makeWebView() {
    return new WebView(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

我调整的线性布局和感动的是,这样的web视图将需要的地方放置在RR1,从我收集。我一直在寻找在不同的文档,它要么wasnt完全是IW作为找或者通过随机性,我只是didnt抓住它。

I resized the Linear Layout and moved that, so the Webview would need to be placed somewhere in RR1, from what i gather. I was looking at various documentation, and it either wasnt quite what iw as looking for or MAYBE by randomness, i just didnt grasp it.

你有想法,这是如何做到的呢?

Do you have ideas as to how this is accomplished?

一点题外话:我不知道为什么onCreateOptionMenu方法是存在的。我不使用它的计划,并会prolly最终删除。

As a sidenote: I have no idea why the onCreateOptionMenu method is there. I dont plan on using it, and will prolly end up removing it.

推荐答案

尝试 rl.addView(WV,0) 将这样的伎俩:)

Try rl.addView(wv, 0); will do the trick :)

这篇关于我试着以编程方式插入的WebView到我的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆