【问题标题】:Set textView text to a remote config default value from layout xml file将 textView 文本设置为布局 xml 文件中的远程配置默认值
【发布时间】:2019-09-10 15:32:57
【问题描述】:

我有一个用例,我需要将存储在远程配置 xml 中的默认值直接存储在我的布局 xml 文件的文本视图中,而不使用 textView.setText() 方法

我知道我可以简单地获取 java 代码中的值,然后使用 setText() 方法将其设置为 textview。但是有没有办法从布局 XML 中获取默认的远程配置值并存储在 textview 中,比如android:text="@string/txt"

这是我的远程配置默认 xml

<?xml version="1.0" encoding="utf-8"?>

<defaultsMap>
    <entry>
        <key>main_activation_cost</key>
        <value>1000</value>
    </entry>
    <entry>
        <key>discount_activation_cost</key>
        <value>500</value>
    </entry>
    <entry>
        <key>total_referral_count</key>
        <value>5</value>
    </entry>
</defaultsMap>

【问题讨论】:

    标签: xml firebase android-layout firebase-remote-config


    【解决方案1】:

    无法在 Android XML 布局中简单地指定远程配置参数。您必须编写代码来获取参数,然后使用该数据填充视图。

    【讨论】:

      【解决方案2】:
      Yes there is a way to bind strings from XML
      
      Declare your XML like below
      
      <layout xmlns...>
      
      <data>
      
          <import type="packagename.constants.IConfigConstants" /> //your remote config 
          constants file
      
              <variable
                  name="firebaseConfig"
                  type="com.google.firebase.remoteconfig.FirebaseRemoteConfig" />
      
      </data>
      
          <androidx.appcompat.widget.AppCompatTextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center_vertical"                                 
          android:layout_marginStart="@dimen/margin_medium"                        
          android:text="@{firebaseConfig.getInstance().getString(IConfigConstants.STRING_KEY)}" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-18
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-20
        • 1970-01-01
        相关资源
        最近更新 更多