如何以编程方式删除WebView中的顶部和底部空白?

时间:2020-01-21 17:53:04

标签: java android android-webview spacing

我的Java代码如下:

public class InfoView extends RelativeLayout
{
  double percent;
  int W = 0, H = 0, w, h, Agent_Id = 0, Button_Layout_H;                       // Total / partial width & height

  @RequiresApi(api = Build.VERSION_CODES.O)
  public InfoView(Context context, OnClickListener InfoView_OK_Listener)
  {
    super(context);

    int statusBarHeight = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0)
    {
      statusBarHeight = getResources().getDimensionPixelSize(resourceId);
//    Out(" statusBarHeight = "+statusBarHeight);                              // 48
    }

//    Button_Layout_H = 128;
    percent = GATELib.getPercent("Button_Layout_H : ", 128, MainActivity.h);   // 128 / 720 = 0.17777777777777778
    Button_Layout_H = (int)Math.ceil(0.17777777 * MainActivity.h);

    LinearLayout Demo_Layout = new LinearLayout(context);
    Demo_Layout.setId(View.generateViewId());
//  Demo_Layout.setBackgroundColor(Color.rgb(88, 188, 218));
//  Demo_Layout.setBackgroundColor(Color.rgb(98, 198, 238));
    Demo_Layout.setBackgroundColor(Color.rgb(238, 238, 238));
    Demo_Layout.setHorizontalGravity(Gravity.CENTER);
    Demo_Layout.setVerticalGravity(Gravity.CENTER);

    h = MainActivity.h - statusBarHeight - MainActivity.actionBarHeight - Button_Layout_H;
    addView(Demo_Layout, LinearLayout.LayoutParams.MATCH_PARENT,h);
//  Out("Demo_Layout.h = "+h+"  H = "+H);
    String html ="<body style='margin: 0'><Center>" +
                 "<Table Width=98% Cellpadding=1 Cellspacing=1>" +
                 "  <Tr><Td Align=Center Colspan=2>How GATE Authentication Works</Td></Tr>" +
                 "  <Tr><Td>1.</Td><Td>Press [ Registration ] button</Td></Tr>" +
                 "  <Tr><Td>2.</Td><Td>Select 4 to 6 symbols/pins to create a passcode, e.g.&nbsp;Ⓘ&nbsp;♥&nbsp;2&nbsp;✉</Td></Tr>" +
                 "  <Tr><Td>3.</Td><Td>Press [ Save ] button to save passcode. Press [ Ok ] to confirm.</Td></Tr>" +
                 "  <Tr><Td>4.</Td><Td>Press [ Login ] button to see 10 buttons, each has 4 symbols.</Td></Tr>" +
                 "  <Tr><Td>5.</Td><Td>Follow the order of your pins [ &nbsp;Ⓘ&nbsp;♥&nbsp;2&nbsp;✉ ], click the buttons with the pins, if a pin is missing, click on any random button for that pin.</Td></Tr>" +
                 "  <Tr><Td>6.</Td><Td>Press [ Enter ] button to login.</Td></Tr>" +
                 "</Table></Center></body>";
    WebView webView = new WebView(context);
    webView.setId(View.generateViewId());
    webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
//  Demo_Layout.addView(textView, 868, 358);

//  percent = GATELib.getPercent("textView.h : ", 358, MainActivity.h);   // 0.49722222
    h = (int)Math.ceil(0.49722222 * MainActivity.h);
    Demo_Layout.addView(webView, LinearLayout.LayoutParams.MATCH_PARENT, h);
//  Out("textView.w = "+w+" textView.h = "+h+"  W = "+W+" H = "+H);

    LinearLayout Button_Layout = new LinearLayout(context);
    Button_Layout.setId(View.generateViewId());
    Button_Layout.setBackgroundColor(Color.rgb(168, 98, 188));
    Button_Layout.setHorizontalGravity(Gravity.CENTER);
    Button_Layout.setVerticalGravity(Gravity.CENTER);

    RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, Button_Layout_H);
//  Out(" H = "+H);

    lp1.addRule(RelativeLayout.BELOW, Demo_Layout.getId());
    addView(Button_Layout, lp1);

    Button OK_Button=new Button(context);
    OK_Button.setText("OK");
    OK_Button.setAllCaps(false);
    OK_Button.setOnClickListener(InfoView_OK_Listener);
    Button_Layout.addView(OK_Button);

//  Out(" End : H = "+H+" [ statusBarHeight = "+statusBarHeight+"  actionBarHeight = "+MainActivity.actionBarHeight+" ]");
  }

  private static void out(String message) { System.out.print(message); }
  private static void Out(String message) { System.out.println(message); }
}

屏幕截图如下:

enter image description here

如何从Java代码中删除顶部和底部的空白区域(用红线标记)?

0 个答案:

没有答案
相关问题