2017-07-18 203 views
0

如何添加共享按钮到我的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

+3

请直接在这里发布您的源代码,而不是链接。 – Abhi

回答

0

在我看来,toll_bar.xml是不必要的。

首先,在activity_main.xml,请更改

<include 
    android:id="@+id/toolbar" 
    layout="@layout/toll_bar" /> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?android:actionBarSize" 
    android:background="@color/colorPrimary"/> 

,创建一个名为toolbar.xml把它放到res/menu目录中的菜单文件。

,这里是新MainActivity.java

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); 
     setSupportActionBar(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); 
    } 

    private boolean isActivityStarted(Intent aIntent) { 
     try { 
      startActivity(aIntent); 
      return true; 
     } catch (ActivityNotFoundException e) { 
      return false; 
     } 
    } 

    public void onClickRateThisApp() { 
     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(); 
      } 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.toolbar, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.menu_about: 
       //click the about button 
       break; 
      case R.id.menu_share: 
       //click the share button 
       break; 
      case R.id.menu_rate: 
       onClickRateThisApp();// param view is unnecessary 
       break; 
     } 
     return true; 
    } 
} 

我希望这将帮助你。如果是这样,请采纳我的答案。:-祝你有美好的一天。

+0

谢谢你的回答,我尝试了不同的解决方案,它工作。 https://www.youtube.com/watch?v=U39RsxrWhIA –

0

您可以添加此代码,这将帮助你。

Intent share = new Intent(android.content.Intent.ACTION_SEND); 
       share.setType("text/plain"); 
       share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 

           startActivity(Intent.createChooser(share, "")); 

这将打开应用程序的多个共享选项。 这有助于我与其他人分享。

相关问题