如何向我的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
答案 0 :(得分: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
目录。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_about"
android:enabled="true"
android:icon="@drawable/icon"
android:title="about"
app:showAsAction="always" />
<item
android:id="@+id/menu_share"
android:enabled="true"
android:icon="@drawable/share"
android:title="share"
app:showAsAction="always" />
<item
android:id="@+id/menu_rate"
android:enabled="true"
android:icon="@drawable/rate"
android:title="rate us"
app:showAsAction="always" />
</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;
}
}
我希望这会对你有所帮助。如果是的话,请采纳我的答案.-D祝你有愉快的一天。
答案 1 :(得分:0)
您可以添加此代码,这对您有帮助。
$sql = "SELECT text,pic
FROM newstable
where type='在线报修上广告'
order by id desc limit 0,1";
这将打开app的多重共享选项。 这有助于我与另一个人分享。