0

我有一个 WebView,我想添加我自己的后退按钮,该 webview 是专门为在应用程序中查看 pdf 而设置的。我已经添加了按钮并且它可以工作,但是我无法调整它的大小或在屏幕上移动它,因此 pdf 控件位于按钮下方并且无法使用。按钮布局参数似乎是这些方面的东西(这不在代码中的任何地方,这就是它看起来的样子)

<ImageButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
     />

这里是活动

public class PDFview extends Activity {

    ImageButton im;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        Intent intent = getIntent();
        String LinkTo = intent.getExtras().getString("link");
        WebView mWebView= new WebView(this);
        im = new ImageButton(this);
        im.setImageResource(R.drawable.back);
        im.setLayoutParams(new LayoutParams(100,100));
        im.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                exitPDF();

            }
        });
        im.setLeft(0);
        im.setTop(200);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo);
        mWebView.addView(im);
        setContentView(mWebView);
    }
4

1 回答 1

0

I have a WebView that I want to add my own back button to

More accurately, you have an activity or fragment in which you want to have a WebView and a back button.

I am unable to resize it or move it around the screen

That's because WebView is not really designed to hold other widgets.

Create an XML layout resource that contains a RelativeLayout, which in turn holds onto your WebView and your ImageButton. Make sure that the ImageButton is the second child of the RelativeLayout (with the WebView being the first), so the ImageButton is higher on the Z-axis and floats over the WebView (since that appears to be what you want). Then, use appropriate RelativeLayout positioning rules to put the ImageButton where you want.

All that being said:

  • Consider not having a back button at all, but instead use Android's own BACK button, by overriding onBackPressed(). Use that while there is Web history available (or whatever you are doing with the button), then call super.onBackPressed() after there's nothing more inside your activity to go "back" to, so the user can exit the activity.

  • If that is unacceptable for whatever reason, consider putting your "back" functionality in an action bar. Having a widget float over top of the WebView means that something the user wants to read will be unreadable, because the button is in the way.

于 2013-08-29T12:06:05.940 回答