gpt4 book ai didi

android - 无法在 Android 中调整 Web View 的大小

转载 作者:行者123 更新时间:2023-11-29 01:44:35 25 4
gpt4 key购买 nike

我正在尝试在 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;
}

}

最佳答案

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

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

});

}
}

关于android - 无法在 Android 中调整 Web View 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22246031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com