0

我想限制在初始化时声明的webView's limit一些内容。pixelswebView

View insertPoint = findViewById(R.id.layout);

WebView web = new WebView(this) ;
web.setBackgroundColor(Color.GRAY);
int lHeight = 200 ;
int lWidth = 200 ;


( (ViewGroup) insertPoint ).addView(web, lWidth, lHeight) ;

web.loadUrl("http://www.google.com");

编辑:

Full Screen 是由那个取的WebView,它不在里面200px*200px

编辑:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/layout" >



</LinearLayout>
4

3 回答 3

1

如果您还添加以下行,您的代码应该可以工作:

web.setWebViewClient(new WebViewClient());

否则,您的代码将启动浏览器(全屏)。例如,如果您单击“返回”,您应该会看到您的灰色框。

于 2013-03-21T07:20:28.173 回答
1

您可以尝试使用此代码设置高度宽度:

            WebView w =new WebView(this);
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            w.setLayoutParams(lp);
            w.getLayoutParams().width = 200;
            w.getLayoutParams().height = 200;
            ( (ViewGroup) insertPoint ).addView(w) ;
于 2013-03-21T05:44:42.570 回答
0

是的,您的代码正在运行检查layout xml您已声明的文件天气layout_widthlayout_height match_parent或者fill_parent如果是这样,则将其更改为wrap_content它会起作用。

把这个布局而不是你的布局如果它工作,那么你的代码没有任何问题,否则你的 xml 有问题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/layout"   >


</LinearLayout>

活动课

 public class MainActivity extends Activity {


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

    }
    public void loadView(){
        View insertPoint = findViewById(R.id.layout);

        WebView web = new WebView(this) ;
        web.setWebViewClient(new WebViewClient());
        web.setBackgroundColor(Color.GRAY);
        int lHeight = 200 ;
        int lWidth = 200 ;


        ((ViewGroup) insertPoint) .addView(web, lWidth, lHeight) ;

        web.loadUrl("http://www.google.com");

    }
于 2013-03-21T06:07:55.927 回答