簡體   English   中英

Xamarin Android綁定並在Visual Studio中使用Java庫

[英]Xamarin Android bind and use Java library in Visual Studio

我想從github將這個浮動動作按鈕庫添加到我的Visual Studio項目中: https : //github.com/Clans/FloatingActionButton

我遵循此描述,建議添加綁定庫(Android)。 https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/binding-a-jar/

我嘗試使用.jar.aar文件進行綁定。 這些文件似乎還可以,但是在導入期間出現以下異常:

BINDINGSGENERATOR : warning BG8601: No packages found

要么

JARTOXML : warning J2X9001: Couldn't load class com/github/clans/fab/FloatingActionButton$1 : java.lang.NoClassDefFoundError: android/view/ViewOutlineProvider

任何幫助表示贊賞。

(這是我做過的最干凈的綁定之一,通常您會進行很多轉換...)

  • 從頭開始,並通過模板創建一個新的Xamarin.Android解決方案/項目。

  • 將Android庫綁定項目添加到您的解決方案中。

  • fab-1.6.3.aar添加到Jars文件夾中:

(我使用了指向 Github項目站點上列出的Maven Central直接鏈接

注意:添加后,請確保將.arr標記為LibraryProjectZip的構建動作。

生成Binding項目,您應該收到有關覆蓋但沒有錯誤的警告。

  • 轉到您的主要Xamarin.Android應用程序項目,並添加對綁定項目的引用。

  • 打開您的Resources / Layout / Main.axml文件並替換內容:


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <com.github.clans.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"/>
</FrameLayout>
  • 在您的仿真器上構建並運行它,還不是很性感,因為沒有代碼可以填充任何東西,但是可以運行:

在此處輸入圖片說明

注意:我繼續並從他們的github示例中導入了所有資源,否則,如果您開始使用他們的示例xml布局和代碼,則會因缺少資源而遇到很多編譯錯誤。

FloatingActionButton/sample/src/main/res:
anim            drawable-xxhdpi     mipmap-xhdpi
drawable        drawable-xxxhdpi    mipmap-xxhdpi
drawable-hdpi       layout          values
drawable-mdpi       menu            values-v21
drawable-v21        mipmap-hdpi     values-w820dp
drawable-xhdpi      mipmap-mdli

並為MainActivity OnCreate和Fragments轉換了足夠的Java代碼來對其進行測試,並且我嘗試的所有方法都可以:

在此處輸入圖片說明

SushiHangovers的答案對您自己進行綁定而言是正確的。 但是,如果您只想使用Clans Floating Action Button庫,則可以通過nuget輕松安裝FAB.XamarinAndroid,它已經為您完成了該庫的綁定。

編輯

GitHub存儲庫 ,您可以在其中查看綁定項目的結構。

暫無
暫無

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

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