2016-06-29 81 views
1

所以我想在我的Android應用程序中實現一個自定義的Facebook分享按鈕,但到目前爲止,我只設法使用本地的,我導入到我的.xml文件,並製作在我的Java活動中使用該特定頁面。自定義Facebook分享按鈕,Android應用程序

我的代碼看起來像這樣(在.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(); 

Inside onCreate();

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

我將如何去利用,我已經導入XML定製按鈕?如果它不是ShareButton的實例,使用.setShareContent顯然不起作用。提前致謝!

+0

找到任何解決方案?我也陷入了同樣的情況。 –

回答

3

我找到了解決辦法。很顯然,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> 

內活動獲得的這些觀點refrences。

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();做的伎倆。

+0

它的方式,但工作!好 – Mrinmoy