我有一个包含两个活动的应用程序。MainActivity
加载一个 xml( printscreen.xml
),其中有两个按钮(print
和close
) 和一个webview
. 当应用程序运行时,它会加载网页和两个位置正确的按钮。当我单击打印按钮时,它会转到另一个活动,当我返回主活动按钮时,它会消失(关闭按钮的某些部分)。
这是我的printscreen.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" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/top"
android:gravity="center" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnClose"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:background="@drawable/btn"
android:text="Close" />
<Button
android:id="@+id/btnPrint"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/btn"
android:text="Print" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableRow
android:id="@+id/tableRow7"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<WebView
android:id="@+id/webViewPrint"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
</LinearLayout>
这是我的问题的屏幕截图:第一个是初始加载应用程序时(它是正确的)。第二个是回到 MainActivity(这是不正确的)。
这是我的主要活动:
public class VividViewerActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mUnityPlayer = new UnityPlayer(this);
context = this;
View playerView = mUnityPlayer.getView();
setContentView(playerView);
playerView.requestFocus();
mJSInterface = new JSInterface();
mWebView = new WebView(this);
FrameLayout layout = new FrameLayout(this);
addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
layout.addView(mWebView, new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
Gravity.NO_GRAVITY));
// Basic settings of WebView.
mWebView.setBackgroundColor(Color.TRANSPARENT);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSupportZoom(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginsEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
});
pv = new PrintView(this, null);
layout.addView(pv, new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
Gravity.NO_GRAVITY));
mWebView.addJavascriptInterface(mJSInterface, "UnityInterface");
pv.wev.addJavascriptInterface(mJSInterface, "UnityInterface");
// Start in invisible state.
mWebView.setVisibility(View.GONE);
pv.setVisibility(View.GONE);
}
// reezo take picture
public void takePictureByCamera() {
/*
* //String path = Environment.getExternalStorageDirectory() +
* "/CameraImages/example.jpg"; //File file = new File("image.jpg");
* //Uri outputFileUri = Uri.fromFile( file ); Intent cameraIntent = new
* Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
* //cameraIntent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
* startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
*/
Intent intent = new Intent(context, CustomCameraActivity.class);
startActivity(intent);
}
// reezo open gallery
public void selectImageFromGallery() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, ACTIVITY_SELECT_IMAGE);
}
public void updateWebView(final String lastRequestedUrl,
final boolean loadRequest, final boolean visibility,
final int leftMargin, final int topMargin, final int rightMargin,
final int bottomMargin) {
// Process load requests.
if (lastRequestedUrl != null && (loadRequest || !mInitialLoad)) {
runOnUiThread(new Runnable() {
public void run() {
Log.e("lastRequestedUrl", lastRequestedUrl);
Log.e("cookie", "URL: " + lastRequestedUrl);
Log.e("cookie", "RETRIEVE COOKIES FROM COOKIESTORE:");
CookieSyncManager cookieSyncManager = CookieSyncManager
.createInstance(mWebView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
List<Cookie> cookies = mHttpclient.getCookieStore()
.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
String cookieString = cookie.getName() + "="
+ cookie.getValue() + "; domain="
+ cookie.getDomain();
cookieManager.setCookie(cookie.getDomain(),
cookieString);
Log.e("cookie", " --" + cookie.getDomain() + " : "
+ cookieString);
}
}
cookieSyncManager.sync();
Log.e("cookie", "NOW! WEBVIEW URL=" + lastRequestedUrl);
mWebView.loadUrl(lastRequestedUrl);
isWebsiteLoaded = true;
// mWebView.loadUrl("file:///android_asset/index.html");
}
});
mInitialLoad = true;
}
// Process changes in margin amounts
if (leftMargin != mLeftMargin || topMargin != mTopMargin
|| rightMargin != mRightMargin || bottomMargin != mBottomMargin) {
mLeftMargin = leftMargin;
mTopMargin = topMargin;
mRightMargin = rightMargin;
mBottomMargin = bottomMargin;
runOnUiThread(new Runnable() {
public void run() {
// Apply a new layout to the WebView.
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
Gravity.NO_GRAVITY);
params.setMargins(mLeftMargin, mTopMargin, mRightMargin,
mBottomMargin);
mWebView.setLayoutParams(params);
}
});
}
// Process changes in visibility.
if (visibility != (mWebView.getVisibility() == View.VISIBLE)) {
runOnUiThread(new Runnable() {
public void run() {
if (visibility) {
// Show and set focus.
mWebView.setVisibility(View.VISIBLE);
mWebView.requestFocus();
} else {
// Hide.
mWebView.setVisibility(View.GONE);
}
}
});
}
}
public void setCookie(String cookie) {
Log.e("cookie", "FROM UNITY: setCookie(): OBSOLATE!!!" + cookie);
this.mCookie = cookie;
}
// reezo call javascript function
public void callJavascriptFunction(final String unityMessage) {
Log.e("unity Message", unityMessage);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mWebView.loadUrl("javascript:alert(" + unityMessage + ")");
}
});
}
public String getPollDeviceAttitude() {
return Constant.rotationValueForCamera;
}
public int getImagePickerStatus() {
Log.e("ImagePickerStatus", Constant.imagePickerStatus
+ " mmmmmmmmmmmmmmmmmmmmm");
if (Constant.imagePickerStatus == 0) {
Constant.imagePickerStatus = -1;
return 0;
} else if (Constant.imagePickerStatus == 1) {
Constant.imagePickerStatus = -1;
return 1;
}
return Constant.imagePickerStatus;
}
public void loadPrintActivity(String printUrl) {
Log.e("JMK", "loadPrintActivity " + printUrl);
/*
* Intent iPrintActivity = new Intent(context, PrintActivity.class);
* Constant.printUrl = printUrl; Constant.isPrintButtonVisible =true;
* startActivity(iPrintActivity);
*/
WebSettings webSettings = pv.wev.getSettings();
webSettings.setSupportZoom(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginsEnabled(true);
pv.wev.setWebViewClient(new WebViewClient() {
});
pv.wev.loadUrl(printUrl);
runOnUiThread(new Runnable() {
public void run() {
// Show and set focus.
pv.setVisibility(View.VISIBLE);
pv.btnPrint.setVisibility(View.VISIBLE);
pv.setClickable(true);
pv.requestFocus();
}
});
pv.btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("VIEWWWWWWWWWWWWWWWWWWWWW", "kaj korce..............");
pv.setVisibility(View.INVISIBLE);
}
});
pv.btnPrint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("VIEWWWWWWWWWWWWWWWWWWWWW", "kaj korce..............");
runOnUiThread(new Runnable() {
public void run() {
// Show and set focus.
pv.btnPrint.setVisibility(View.INVISIBLE);
//String mPath = Environment
//.getExternalStorageDirectory().toString()
// + "/"
//+ "dse.jpg";
/*
* Bitmap bitmap; View v1 = pv.wev.getRootView();
* v1.setDrawingCacheEnabled(true);
*
* bitmap = Bitmap.createBitmap(v1.getDrawingCache());
* v1.setDrawingCacheEnabled(false); OutputStream fout =
* null; File imageFile = new File(mPath); try { fout =
* new FileOutputStream(imageFile);
* bitmap.compress(Bitmap.CompressFormat.JPEG, 40,
* fout); fout.flush(); fout.close();
*
* } catch (FileNotFoundException e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
* catch (IOException e) { // TODO Auto-generated catch
* block e.printStackTrace(); }
*/
Picture picture = pv.wev.capturePicture();
Bitmap b = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
ByteArrayOutputStream out = null;
byte[] byteArchadeImage = null;
try {
out = new ByteArrayOutputStream();
if (out != null) {
b.compress(Bitmap.CompressFormat.JPEG, 100, out);
byteArchadeImage = out.toByteArray();
out.flush();
out.close();
}
} catch (Exception e) {
Log.e("MSG", e.getLocalizedMessage());
}
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream(Environment
.getExternalStorageDirectory()
.toString()
+ "/" + "dse.pdf"));
document.open();
//document.add(new Paragraph("This Is For You!!!!"));
try {
Image image = Image.getInstance(byteArchadeImage);
image.scaleAbsolute(600,
800);
image.setAlignment(Image.MIDDLE |Image.ALIGN_MIDDLE);
document.add(image);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
Intent printIntent = new Intent(
VividViewerActivity.this,
PrintDialogActivity.class);
Uri uri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory().toString()
+ "/"
+ "dse.pdf"));
printIntent.setDataAndType(uri, "application/pdf");
// printIntent.putExtra(Intent.EXTRA_STREAM,
// Uri.fromFile(new
// File(ur)));
String time = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
printIntent.putExtra("title", "vividworks_report_"+time+".pdf");
startActivity(printIntent);
}
});
}
});
}
private int dpToPx(int dp) { float density =
getApplicationContext().getResources() .getDisplayMetrics().density;
return Math.round((float) dp * density); }
public void LoadPrintActivityWithOutPrint(String printUrl) {
Log.e("JMK", "LoadPrintActivityWithOutPrint " + printUrl);
/*
* Intent iPrintActivity = new Intent(context, PrintActivity.class);
* Constant.printUrl = printUrl; Constant.isPrintButtonVisible =false;
* startActivity(iPrintActivity);
*/
WebSettings webSettings = pv.wev.getSettings();
webSettings.setSupportZoom(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginsEnabled(true);
pv.wev.setWebViewClient(new WebViewClient() {
});
pv.wev.loadUrl(printUrl);
runOnUiThread(new Runnable() {
public void run() {
// Show and set focus.
pv.setVisibility(View.VISIBLE);
pv.btnPrint.setVisibility(View.INVISIBLE);
pv.setClickable(true);
pv.requestFocus();
}
});
pv.btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("VIEWWWWWWWWWWWWWWWWWWWWW", "kaj korce..............");
pv.setVisibility(View.INVISIBLE);
}
});
}
public void loadNewWebPage(String webPage) {
Log.e("jari", "loadNewWebPage " + webPage);
// Constant.printUrl = "https://www.facebook.com/";
Constant.printUrl = webPage;
pv.wev.loadUrl(Constant.printUrl);
}
protected void onDestroy ()
{
super.onDestroy();
mUnityPlayer.quit();
}
// onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume.
protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
if (isFinishing())
mUnityPlayer.quit();
}
protected void onResume()
{
super.onResume();
mUnityPlayer.resume();}
关闭按钮(在第二张图片中)在返回 mainactivity 后如何占据相同的位置。请帮我。
我的解决方案:
仅更改布局文件。这是更新的 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" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/top"
android:gravity="center" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/btnClose"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:background="@drawable/btn"
android:text="Close" />
<Button
android:id="@+id/btnPrint"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/btn"
android:text="Print" />
</RelativeLayout>
</TableRow>
</TableLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableRow
android:id="@+id/tableRow7"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<WebView
android:id="@+id/webViewPrint"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>