如何添加共享按钮到我的Android应用程序,我尝试了几个解决方案在这里,似乎没有任何工作。如何添加共享按钮到我的Android应用程序
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.package.name.MainActivity"
android:weightSum="1">
<include
android:id="@+id/toolbar"
layout="@layout/toll_bar"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="368dp"
android:layout_height="495dp"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
tools:ignore="MergeRootFrame">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="383dp"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_marginTop="30dp"
/>
</FrameLayout>
</LinearLayout>
Toll_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="@string/app_name"
android:textAlignment="viewStart"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textColorHighlight="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/about"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="About"
android:background="@drawable/icon"
android:layout_marginLeft="127dp"
/>
<Button
android:id="@+id/share"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="share"
android:background="@drawable/share"
android:layout_marginLeft="10dp"
/>
<Button
android:id="@+id/rate"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="Rate Us"
android:background="@drawable/rate"
android:layout_marginLeft="10dp"
/>
</android.support.v7.widget.Toolbar>
Main_Activity.xml
package com.package.name;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.toolbar);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://beta.html5test.com/");
mWebView.loadUrl("file:///android_asset/Html36289/index.html");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView .getSettings().setJavaScriptEnabled(true);
Button btnRate = (Button) findViewById(R.id.rate);
btnRate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
onClickRateThisApp(v);
}});
}
private boolean isActivityStarted(Intent aIntent) {
try
{
startActivity(aIntent);
return true;
}
catch (ActivityNotFoundException e)
{
return false;
}
}
public void onClickRateThisApp(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
if (!isActivityStarted(intent)) {
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
if (!isActivityStarted(intent)) {
Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
}
}
}
}
谢谢你的回答,我尝试了不同的解决方案,它的工作。 youtube.com/watch?v=U39RsxrWhIA
请直接在这里发布您的源代码,而不是链接。 – Abhi