【问题标题】:How to restrict the WebView inside in android program如何在android程序中限制WebView
【发布时间】:2013-03-21 16:48:33
【问题描述】:

我想将webView'slimit 限制在pixels 内,这些pixels 是在webView 初始化时声明的。

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

已编辑:

全屏由 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>

【问题讨论】:

  • 全屏由那个 WebView 拍摄,它不在 200px*200px 内。
  • 如果可能发布完整代码
  • @Abhijit .... 请参阅 Luksprog ,评论!

标签: android android-webview


【解决方案1】:

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

web.setWebViewClient(new WebViewClient());

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

【讨论】:

    【解决方案2】:

    你可以试试这个代码来设置高度宽度:

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

    【讨论】:

    • 请尝试在您的 xml 中将 webview 中的高度和宽度设置为 200 dp
    【解决方案3】:

    是的,您的代码正在运行检查 layout xml 文件天气,您已声明您的 layout_widthlayout_height match_parentfill_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");
    
        }
    

    【讨论】:

    • 拜托,你能给我一些代码吗?如果我理解正确的话...我已经实现了 layout_height 和 width 到 wrap_content 我也发布了我的 Liner_layout xml 文件。
    • 我已经编辑了我的答案,告诉我天气它工作与否。
    • 抱歉,web_page 仍然占用了整个空间,但是……这是否在您的机器上工作,或者我的 240*320 像素的 AVD 有问题!
    • 1024 * 768 是你的分辨率没关系检查你的 android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizo​​ntal_margin" android:paddingRight="@dimen/activity_horizo​​ntal_margin" android :paddingTop="@dimen/activity_vertical_margin" 工具:context=".MainActivity" 东西
    • 我已经复制粘贴了您的代码,但仍然无法正常工作……是时候拿起枪射击我的机器了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2013-01-21
    • 2012-04-11
    相关资源
    最近更新 更多