2017-03-27 12 views
1

gradle Iではデータバインディングが有効です。私はアプリを実行すると、背景画像を変更しようとした後、私は、ログに次のエラーを観察:データバインディングを使用して背景画像を変更する

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. 
java.lang.RuntimeException: Found data binding errors. 
****/ data binding error ****msg:Identifiers must have user defined types from the XML file. ImageBacground is missing it 
file:D:\Bogv\Android project\Pote\app\src\main\res\layout\fragment1.xml 
loc:13:19 - 13:32 
****\ data binding error **** 

マイビュークラス:

public class ImageBacground { 
    private ImageView imageView; 

    public ImageBacground(ImageView imageView) { 
     this.imageView = imageView; 

    } 
    @BindingAdapter({"android:src"}) 
    public static void loadImage(ImageView view, String imageUrl) { 
     Picasso.with(view.getContext()) 
       .load(imageUrl) 
       .placeholder(R.drawable.potehki_fon) 
       .into(view); 
    } 

    public ImageView getImageView() { 
     return imageView; 
    } 

    public void setImageView(ImageView imageView) { 
     this.imageView = imageView; 
    } 
} 

XML

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <data> 
     <import type="android.view.View" /> 
     <variable name="images" type="com.retrofa.potehkilulibalse.images.ImageBacground"/> 
    </data> 
<LinearLayout 
    android:id="@+id/first_activity" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="2dp" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:src="@{ImageBacground.loadImage,[email protected]/potehki_fon}" 
    android:theme="@style/AppTheme.NoActionBar" 

    > 

    <ImageView 

     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="50dp" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:src="@drawable/eda" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="70dp" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:src="@drawable/umivanie" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="50dp" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:src="@drawable/son" /> 
</LinearLayout> 
</layout> 
+0

行うには、2つの方法でXMLレイアウトを結合し、この

activity_main.xml今

<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:imageUrl="@{@drawable/ic_android_black_24dp}"> </LinearLayout> </layout> 

のように使用することができますそれは1)あなたのJavaコードであなたのimageViewのIDを見つけることができ、picassoを使用してimageUrlを設定することができますデータバインディングを使用して。 –

+0

もう一つの方法は、imageUrlをXMLコードにバインドするのを忘れたことです。最初にimageUrlをImageViewウィジェットにバインドします。 –

答えて

1

カスタムバインディングアダプタを作成する必要があります

public class CustomBinding { 

    @BindingAdapter({"imageUrl"}) 
    public static void loadImage(LinearLayout layout, Drawable drawable) { 
     if (drawable != null) { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
       layout.setBackground(drawable); 
      } else { 
       layout.setBackgroundDrawable(drawable); 
      } 
     } 
    } 
} 

その後XMLにあなたがそこに活動ファイルのonCreate()方法

DataBindingUtil.setContentView(this, R.layout.activity_main); 
+0

LinearLayoutに背景を設定したいのですが、ImageView –

+0

ではなく、タグを追加する必要がありますか? –

+0

いいえ、何もしていない場合はお知らせください。私はAndroid Studioでこのコードをテストしていません。私は直接ここに答えを書いた。 –

関連する問題