2016-06-29 6 views
1

私は自分のAndroidアプリにカスタムFacebook共有ボタンを実装したいと思いますが、これまでのところ私は自分の.xmlファイルにインポートしたその特定のページに対するJavaのアクティビティの使用。カスタムFacebook共有ボタン、Android App

私のコードは(.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にインポートしたカスタムボタンを使用するにはどうすればよいですか? .setShareContentを使用することは明らかにShareButtonのインスタンスではない場合は機能しません。前もって感謝します!

+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を取得し、私のレイアウトでは

:ここ

は私がこれを行ってきた方法です。 onClickの内部

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

()

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

関連する問題