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この問題についてのいくつかの記事が、決定的な答えかあります。アプローチは機能しませんでした。確かにこれは一般的な実装です。助けを前にありがとう。
は、「確かにこれが一般的です実装がされて」 - 、データバインディングはまだリリース候補であることを考慮します私はデータバインディングに関連する*何も「共通」とは考えていません。この方法で高さと幅を設定することができない場合は、レイアウトを拡張するとすぐにそのデータが検証され、その時点でモデルを挿入していなくても、その属性を設定することはできません。バインディング式を適用する合成プロパティ(たとえば 'app:width')を使用して、ビューの' LayoutParams'を調整するいくつかの回避策がある可能性があります。 – CommonsWare
あなたは本当に正しいかもしれません。私は、他のプラットフォーム(Windows)上のデータバインディングに精通していたので、私はそれがAndroid上でも一般的であると予想したばかりです。 –