2016-06-29 5 views
1

Android 앱에 맞춤 Facebook 공유 버튼을 구현하고 싶지만, 지금까지는 필자가 .xml 파일로 가져 와서 만든 고유 이름을 사용하여 관리했습니다. 해당 특정 페이지에 대한 Java 활동에서의 사용.사용자 정의 Facebook Share-Button, 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

해결책을 찾았습니다. 페이스 북 공유가 자체 버튼 클릭 이벤트에서 작동한다는 것이 분명합니다. 따라서 명시 적으로 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