-2

我编写了可以在 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)当我在我的设备上运行项目时,我喜欢这个屏幕(

在此处输入图像描述)当我单击设备上的菜单按钮时,我可以显示设置菜单,可以创建自己的菜单。例如,我想创建我的按钮,可以像静态菜单的缩放按钮一样缩放,谢谢

4

1 回答 1

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


    }

}
于 2014-01-23T12:03:00.900 回答