包括一个TextView和覆盖文本文本、TextView

2023-09-06 07:35:56 作者:大鼻涕嘎嘎

我有一个TextView我为我的菜单页的标题是:

I have a TextView I use as the headline of my menu page:

<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的文本,包括在每个子菜单视图,只覆盖文本。

Now I need a TextView with the same color, size and style on every sub menu in my app. Instead of copy pasting the whole TextView to every layout and just change the text in each one I thought I'd make one layout with the TextView and include it in every sub menu view, only overriding the text.

我的code是这样的:

My code looks like this:

/layout/menutextview.xml:

/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文件试图重写文本属性:

The includes in each layout xml file tries to override the text attribute:

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

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

但是默认的文本显示无处不在。任何人有什么样的问题可能是一个想法?

But the default text is displayed everywhere. Anyone have an idéa of what the problem might be?

问候, 马蒂亚斯

推荐答案

欢迎到计算器;)

包括不能被用来overrride孩子的属性。它不知道哪种类型的布局的,你会包括,它只会膨胀并将其添加到当前布局。

Include cannot be used to "overrride" children properties. It doesn't know which type of layout you will include, it will only inflate it and add it to the current layout.

要动态地更改文本,你需要做的是在code。

To dynamically change the text, you need to do it in code.

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