How to restrict the WebView inside in android program

ε祈祈猫儿з 提交于 2019-12-25 01:51:21

问题


i want to restrict webView's limit inside some pixels which is have declared at the time of webView initialization .

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

Edited :

Full Screen is taken by that WebView , it is not residing inside 200px*200px .

Edited :

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

回答1:


Your code should work if you also add the line:

web.setWebViewClient(new WebViewClient());

Otherwise your code will start the browser(which is full screen). If you click BACK for example you should see your gray box.




回答2:


You can try this code for setting the height width :

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



回答3:


Yes your code is working check layout xml file weather you have declared your layout_width,layout_height match_parent or fill_parent if so then change it to wrap_content it will work .

put this layout instead of your layout if it works then there is nothing wrong in your code otherwise u have problem in ur 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>

Activity class

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

    }


来源:https://stackoverflow.com/questions/15540265/how-to-restrict-the-webview-inside-in-android-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!