【问题标题】:Include a TextView and override the text包含一个 TextView 并覆盖文本
【发布时间】:2012-02-03 10:19:10
【问题描述】:

我有一个 TextView 用作菜单页的标题:

<TextView
  android:id="@+id/menuTextView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Menu"
  android:textColor="@color/white"
  android:textSize="25sp"
  android:textStyle="bold" />

现在我需要在我的应用程序的每个子菜单上使用相同颜色、大小和样式的 TextView。与其将整个 TextView 复制粘贴到每个布局中并只更改每个布局中的文本,我想我会使用 TextView 制作一个布局并将其包含在每个子菜单视图中,仅覆盖文本。

我的代码如下所示:

/layout/menutextview.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/menuTextView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/default"
  android:textColor="@color/white"
  android:textSize="25sp"
  android:textStyle="bold" />

每个布局 xml 文件中的包含尝试覆盖文本属性:

<include layout="@layout/menutextview" android:text="@string/menu" />

<include layout="@layout/menutextview" android:text="@string/settings" />

但默认文本显示在任何地方。任何人都知道问题可能是什么?

问候, 马蒂亚斯

【问题讨论】:

    标签: android include textview overriding


    【解决方案1】:

    Include 不能用于“覆盖”子属性。它不知道您将包含哪种类型的布局,它只会将其膨胀并将其添加到当前布局中。

    要动态更改文本,需要在代码中进行。

    final TextView textView1 = (TextView) findViewById(R.id.menuTextView);
    textView1.setText(R.string.menu);
    
    final TextView textView2 = (TextView) findViewById(R.id.settingsTextView);
    textView2.setText(R.string.settings);
    

    【讨论】:

    • 这是您可用于解决此问题的最简单且维护友好的解决方案。
    • 是否没有自定义字段可以让我将任何我想要的内容从 xml 传递到代码?像这样: 然后在代码中我可以去 ((TextView) findViewById(R.id.menuTextView)).setText(getCustom( ));
    • 我想要和你一样,根据文档,你只能覆盖布局(和 id)属性。
    • @STTLCU 我不同意。出于多种原因,应用样式的东西应尽可能保留在 XML 中,因此建议样式的其他答案更好。
    • 确实如此@popovitsj 请记住,当这个答案发布时(以及我的评论),android 还年轻得多,我想与现在有很大不同。
    【解决方案2】:

    尝试使用样式并让 TextView 实现该样式。这样可以更轻松地保持视图的一致性。

    【讨论】:

      【解决方案3】:

      您可以通过DataBinding 实现此目的。首先,您在子布局中定义一个变量:

      <?xml version="1.0" encoding="utf-8"?>
      <layout xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:android="http://schemas.android.com/apk/res/android">
      
          <data>
              <variable
                      name="buttonText"
                      type="String" />
          </data>
      
              <android.support.v7.widget.AppCompatTextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@{buttonText}"/>
      </layout>
      

      然后将其设置在包含它的其他布局文件中:

      <!-- .... other views -->
      <include
          layout="@layout/inc_icon_button"
          bind:buttonText="@{`Put your String here`}" />
      <!-- .... other views -->
      

      最好的情况是你的父布局中也有一个变量,然后就可以转发绑定。

      【讨论】:

      • 显然我无法删除我的赞成票。这个答案缺少的是:++父布局和包含布局的根元素必须是&lt;layout&gt;; ++ &lt;include&gt; 不能是 &lt;layout&gt;&lt;merge&gt; 的直接子代; ++ 你必须使用DataBindingUtil.inflateDataBindingUtil.setContentView 而不是普通的LayoutInflater.inflateActivity.setContentView。否则它会很有帮助,但我花了很长时间才弄清楚为什么我的代码不起作用。
      • 显然我不能再编辑我的评论了。如果有人想知道如何从DataBindingUtil.inflate 获取视图:View rootView = DataBindingUtil.inflate(inflater, R.layout.my_layout, container, false).getRoot();
      • 注意,要使“绑定”属性出现,您需要在布局标签中包含xmlns:bind=”http://schemas.android.com/apk/res-auto
      【解决方案4】:

      您可以使用以下解决方案:

      1. 给包含标签和包含布局中的TextView一个特定的ID(如“节”)
      2. 在代码View section;TextView textview; 中将包含标记声明为视图和TextView
      3. 将视图与您的包含section = findViewById(R.id.section); 的ID 绑定
      4. View.findViewById();绑定你的包含的TextView

      textview = section.findViewById(R.id.textview);

      我使用了this方面的信息。

      【讨论】:

        猜你喜欢
        • 2019-12-27
        • 1970-01-01
        • 2015-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多