自定义Facebook Share-Button,Android App

Nyf*_*ul 1 java xml android facebook

所以我想在我的Android应用程序中实现一个自定义的Facebook共享按钮,但到目前为止我只设法使用本机的,我导入到我的.xml文件中并在我的Java活动中使用该特定页面.

我的代码看起来像这样(在.xml中);

<com.facebook.share.widget.ShareButton
            android:id="@+id/share_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Share"
            android:layout_centerHorizontal="true"/>
Run Code Online (Sandbox Code Playgroud)

在我的.java中,在onCreate()之外;

private ShareButton shareButton;

ShareLinkContent content = new ShareLinkContent.Builder()
        .setContentUrl(Uri.parse("https://developers.facebook.com"))
        .setContentTitle("MyTitle")
        .build();
Run Code Online (Sandbox Code Playgroud)

在onCreate()中;

    shareButton = (ShareButton)findViewById(R.id.share_btn);
    shareButton.setShareContent(content);
Run Code Online (Sandbox Code Playgroud)

我如何使用我导入XML的自定义按钮?如果它不是ShareButton的实例,那么使用.setShareContent显然不起作用.提前致谢!

Dee*_*ore 7

我找到了解决方案.很明显,facebook分享将在自己的按钮点击事件中运行.因此,您必须显式调用该View.performclick()方法.

这是我如何做到这一点:

在我的布局

<com.facebook.share.widget.ShareButton
        android:id="@+id/share_btn_fb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:contentDescription="@string/share" />

    <LinearLayout
        android:id="@+id/share_btn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:src="@drawable/google_share" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/share_google"
            android:textColor="@color/green_color"
            android:layout_marginLeft="5dp"
            android:textSize="18sp" />
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

内部活动获得这些观点的参考.

  ShareButton  shareButton = (ShareButton) layout.findViewById(R.id.share_btn_fb);
  LinearLayout shareFB = (LinearLayout) layout.findViewById(R.id.share_btn);
  LinearLayout shareGoogle = (LinearLayout) layout.findViewById(R.id.share_google);
  shareGoogle.setOnClickListener(this);
  shareFB.setOnClickListener(this);
Run Code Online (Sandbox Code Playgroud)

在onClick()里面

case R.id.share_btn :
                shareButton.performClick();
                break;
            case R.id.share_btn_fb :
                ShareLinkContent content = new ShareLinkContent.Builder()
                        .setContentTitle("Content title")
                        .setContentDescription("App descr")
                        .setContentUrl(Uri.parse("some url"))
                        .build();
                shareButton.setShareContent(content);
                break;
Run Code Online (Sandbox Code Playgroud)

基本上shareButton.performClick();是在做伎俩.