无法在Android中调整Web视图的大小

时间:2014-03-07 09:20:55

标签: android webview resize

我正在尝试调整android中的Web View。在第一次单击时,Webview将扩展为新大小,但在第二次单击时,它不会恢复为我指定的旧大小。任何帮助表示赞赏。

public class MainActivity extends Activity {
        RelativeLayout layout;
        Boolean Expanded=true;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            layout=(RelativeLayout)findViewById(R.id.test);




            final WebView web = new WebView(this);
            web.setId(1);
            web.getSettings().setJavaScriptEnabled(true);
            web.loadUrl("https://www.google.co.in");
            layout.addView(web, new LayoutParams(500,250));

            web.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    // TODO Auto-generated method stub
                    if(Expanded=true)
                    {

                        Expanded=false;
                        layout.removeView(web);
                        layout.addView(web, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
                    }
                    else if(Expanded=false)
                    {
                        Expanded=true;
                        layout.removeView(web);
                        layout.addView(web, new LayoutParams(500,250));
                    }


                    return false;
                }
            });

        }

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

    }

2 个答案:

答案 0 :(得分:1)

这是我执行的代码...按预期运行...

public class MainActivity extends Activity {

    RelativeLayout layout;
    boolean Expanded = false;

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

        layout = (RelativeLayout) findViewById(R.id.layout);

        final RelativeLayout.LayoutParams webViewParams = new RelativeLayout.LayoutParams(500, 250);
        webViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

        final RelativeLayout.LayoutParams webViewNewParams = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        final WebView myBrowser = new WebView(this);

        myBrowser.loadUrl("https://www.google.co.in");

        layout.addView(myBrowser, webViewParams);
        myBrowser.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                if (arg1.getAction() == MotionEvent.ACTION_UP) {
                    if (Expanded) {
                        myBrowser.setLayoutParams(webViewParams);
                        Expanded = false;
                    } else {
                        myBrowser.setLayoutParams(webViewNewParams);
                        Expanded = true;
                    }
                }
                return false;
            }

        });

    }
}

答案 1 :(得分:0)

首先使用if(Expanded==false)

检查您的if条件,因为这不是有效条件