Android创建自己的pdf阅读器

时间:2014-01-23 11:51:15

标签: android pdf pdf-reader

我编写的代码可以在sdcard中打开pdf文件。但我的问题是有没有可能使用Xml文件在自己的布局中查看Pdf文件而不是静态到Java文件 这是我的代码

public class MainActivity extends Activity {

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

    Button button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            File pdfFile = new File(Environment
                    .getExternalStorageDirectory(), "test.pdf");

            try {
                if (pdfFile.exists()) {
                    Uri path = Uri.fromFile(pdfFile);
                    Intent objIntent = new Intent(Intent.ACTION_VIEW);
                    objIntent.setDataAndType(path, "application/pdf");
                    objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(objIntent);
                } else {
                    Toast.makeText(MainActivity.this, "File NotFound",
                            Toast.LENGTH_SHORT).show();
                }
            } catch (ActivityNotFoundException e) {
                Toast.makeText(MainActivity.this,
                        "No Viewer Application Found", Toast.LENGTH_SHORT)
                        .show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

}

}

我还有第二个问题 2)当我在我的设备上运行项目时,我喜欢这个屏幕(

enter image description here)当我点击设备上的菜单按钮时,我可以显示设置菜单 可以创建我自己的菜单。例如,我想创建我的按钮,可以像静态菜单的缩放按钮一样缩放 感谢

1 个答案:

答案 0 :(得分:0)

use webview in different layout in  our application like this::---->


webview.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to http://www.google.com" />

</LinearLayout>


mainactivity.java 

public class MainActivity extends Activity {

    private Button button;

    public void onCreate(Bundle savedInstanceState) {
        final Context context = this;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonUrl);

        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url","www.google.com")
            startActivity(intent);
          }

        });

    }

}


webviewactivity.java

public class WebViewActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
                if (getIntent().hasExtra("url")) {
                    webView.loadUrl("http://www.google.com");
                }


    }

}