1
ここに私のxmlレイアウト:javaファイルから変数の値を設定することはできますか?
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="clickHandler"
type="com.myproject.android.customer.ui.adapter.MyHandler" />
<variable
name="item"
type="com.myproject.android.customer.api.model.Merchant" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageViewPhoto"
android:layout_width="@dimen/preview_image_height"
android:layout_height="@dimen/preview_image_height"
android:onClick="clickHandler.onClickImageViewFavorite()"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
</layout>
OK。それはうまくいく。 質問:
- それはjavaファイルからXMLに変数test_variableの値を設定することは可能ですか?
- この変数の値をandroid:layout_widthに設定しますか?
このようSomithing:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="test_variable" type="Integer"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageViewPhoto"
android:layout_width="@{test_variable}"
android:layout_height="@dimen/preview_image_height"/>
</android.support.constraint.ConstraintLayout>
</layout>
を次のよう
Activity
からheight
と要素のwidth
を設定することができますまた、あなたが心配する必要はありません**高さ** XMLファイルのみ。私はこれのためにJavaコードを使用するwan't '。 – Alex