2017-12-01 4 views
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。それはうまくいく。 質問:

  1. それはjavaファイルからXMLに変数test_variableの値を設定することは可能ですか?
  2. この変数の値を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> 

答えて

0

あなたは私が設定したい

textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
+0

を次のようActivityからheightと要素のwidthを設定することができますまた、あなたが心配する必要はありません**高さ** XMLファイルのみ。私はこれのためにJavaコードを使用するwan't '。 – Alex

関連する問題