簡體   English   中英

自定義Facebook共享按鈕,Android應用

[英]Custom Facebook Share-Button, Android App

因此,我想在我的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"/>

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

private ShareButton shareButton;

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

里面的onCreate();

    shareButton = (ShareButton)findViewById(R.id.share_btn);
    shareButton.setShareContent(content);

如何使用已導入XML的自定義按鈕? 如果它不是ShareButton的實例,則顯然無法使用.setShareContent。 提前致謝!

我找到了解決方案。 很明顯,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>

內部活動反映了這些觀點。

  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);

內部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;

基本上是shareButton.performClick(); 正在解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM