2016-02-09 6 views
8

EditTextのheightプロパティを動的に設定できる必要があります。私は、アプリケーション全体で他のプロパティのデータバインディングを使用しているので、データバインディングを使用して要素の高さを制御できるようにしたいと考えています。ここに私のxmlのストリップダウンバージョンは次のとおりです。Androidデータバインディングlayout_widthとlayout_height

public class LoginViewModel extends BaseObservable { 
public final ObservableField<String> verificationCode; 
public final ObservableField<Boolean> compact; 

@Bindable 
public String getVerificationCode() { 
    if (this.verificationCode == null) { 
     return ""; 
    } else { 
     return this.verificationCode.get(); 
    } 
} 

public void setVerificationCode(String verificationCode) { 
    this.verificationCode.set(verificationCode); 
    invalidateProperties(); 
} 

@Bindable 
public Boolean getCompact(){return this.compact.get();} 

public void setCompact(Boolean value) 
{ 
    this.compact.set(value); 
    this.invalidateProperties(); 
} 

@BindingAdapter("android:layout_height") 
public static void setLayoutHeight(EditText view, float height) 
{ 
    ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); 
    layoutParams.height = (int)height; 
    view.setLayoutParams(layoutParams); 
} 

public LoginViewModel(Context ctx) { 
    verificationCode = new ObservableField(); 
    compact = new ObservableField(); 
} 

寸法はdimens.xmlファイルに格納されています

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<data> 
    <variable name="loginVM" type="com.testing.stuff.ViewModels.LoginViewModel" /> 
</data> 

<EditText android:inputType="number" 
      android:id="@+id/txtVerificationCode" 
      android:layout_height="@{loginVM.compact ? @dimen/verificationHeightCompact : @dimen/verificationHeightFull}" 
      android:layout_width="match_parent" 
      android:paddingRight="16dp" 
      android:paddingLeft="16dp" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:layout_marginLeft="10dp" 
      android:alpha="@{loginVM.verificationOpacity}" 
      android:layout_marginStart="10dp" 
      android:textAlignment="center" 
      android:visibility="visible" 
      android:hint="Enter verificationCode" 
      android:text="@{loginVM.verificationCode}" /> 
</layout> 

そして、ここでは私のビューモデルのストリップダウンバージョンです。そして、私はビューモデルのプロパティを変更しています。しかし、私がアプリを起動すると、起動直後に次のエラーが出ます(bindingadapterはデバッグ時に起動しません)。私は、画面上の他のいくつかの要素を持っているが、この特定の一つは、私は特定のアクションが発生したときの高さを変更する必要があるものです:

FATAL EXCEPTION: main 

Process: com.testing.stuff, PID: 32752 

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.testing.stuff/com.testing.stuff.Login}: 
java.lang.RuntimeException: Binary XML file line #69: You must supply 
a layout_height attribute. 

Caused by: java.lang.RuntimeException: Binary XML file line #69: You 
must supply a layout_height attribute. 

SOこの問題についてのいくつかの記事が、決定的な答えかあります。アプローチは機能しませんでした。確かにこれは一般的な実装です。助けを前にありがとう。

+1

は、「確かにこれが一般的です実装がされて」 - 、データバインディングはまだリリース候補であることを考慮します私はデータバインディングに関連する*何も「共通」とは考えていません。この方法で高さと幅を設定することができない場合は、レイアウトを拡張するとすぐにそのデータが検証され、その時点でモデルを挿入していなくても、その属性を設定することはできません。バインディング式を適用する合成プロパティ(たとえば 'app:width')を使用して、ビューの' LayoutParams'を調整するいくつかの回避策がある可能性があります。 – CommonsWare

+0

あなたは本当に正しいかもしれません。私は、他のプラットフォーム(Windows)上のデータバインディングに精通していたので、私はそれがAndroid上でも一般的であると予想したばかりです。 –

答えて

18

データバインディングを使用する場合、XMLから値を削除します。この問題を回避するために、削除するときに使用するデフォルト値を追加することができます。

参照:http://developer.android.com/tools/data-binding/guide.html(ページの下部)。

android:layout_height="@{loginVM.compact ? @dimen/verificationHeightCompact : @dimen/verificationHeightFull, default=wrap_content}" 
+0

私は他のSOスレッドでこれを見ませんでした。本当にありがとう! –

+2

エラー:(13,34)android.widget.LinearLayoutにパラメータ型floatの属性 'android:layout_width'のセッターが見つかりません。 ' –

+0

@GuilhermeTorresCastro私の答えを確認してください –

1

Androidの課題追跡についての議論によれば、データがカスタムバインディングのアダプタを作成せずに結合してレイアウトの高さや幅を設定することは不可能である。

https://code.google.com/p/android/issues/detail?id=180666

ビューを設定するために必要な結合アダプタ高さは、そのようになります。

@BindingAdapter("android:layout_height") 
public static void setLayoutHeight(View view, int height) { 
    LayoutParams layoutParams = view.getLayoutParams(); 
    layoutParams.height = height; 
    view.setLayoutParams(layoutParams); 
} 
8

Javaの

で0
@BindingAdapter("layout_height") 
public static void setLayoutHeight(View view, float height) { 
    LayoutParams layoutParams = view.getLayoutParams(); 
    layoutParams.height = height; 
    view.setLayoutParams(layoutParams); 
} 

そして、あなたのXMLで は

app:layout_height="@{ viewModel.isBig ? @dimen/dp_20 : @dimen/dp_5 }" 

インポートは、このようなアプリは

xmlns:app="http://schemas.android.com/apk/res-auto" 
+1

これは正解でなければなりません – Badjano

+1

私に思い出させてくれてありがとう! – Badjano

+0

androidを動作させるためのデフォルト値を追加する必要がありました:layout_height = "@ {smallScreen?@ dimen/dp_20:@dimen/dp_5、default = wrap_content}" – Shubham

関連する問題