android - 包含一个 TextView 并覆盖文本

标签 android include textview overriding

我有一个 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 制作一个布局并将其包含在每个子菜单 View 中,仅覆盖文本。

我的代码如下所示:

/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" />

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

问候, 马蒂亚斯

最佳答案

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);

关于android - 包含一个 TextView 并覆盖文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9127013/

相关文章:

qt - 如何使 Qt 项目文件 (.pro) 依赖于平台?

android - 文本切换器更新 TextView 颜色

android - TextView编程中如何设置撇号

android - 如何在 Android 中获取位置及其温度?

android - 共享首选项的类转换异常

安卓自定义 View : Set canvas size to wrap bitmap

include - 创建包含文件时出错 缺少 REPORT/PROGRAM 语句

javascript - Zend Framework - 包含 JS 不适用于新系统

Android setMeasuredDimension - 重力被打破

android - 从android手机获取电话号码