gpt4 book ai didi

java - 在 MainActivity.java 中更改 content_main.xml 上的 TextView

转载 作者:行者123 更新时间:2023-11-30 10:38:09 25 4
gpt4 key购买 nike

我正在使用包含 content_main.xml 布局的 Android Studio 标准模板“Navigation Drawer Activity”。

在这个布局上是我的 textView,我想从我的 MainActivity.java 更改文本。

我试过以下但没有任何变化:

LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.content_main, null);
TextView txt = (TextView)view.findViewById(R.id.txt_head);
txt.setText("sdfsdf");

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativelayout_for_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.w4ter.ledcontroldesign.MainActivity"
tools:showIn="@layout/app_bar_main"
android:orientation="vertical"
android:paddingTop="10dp">

<TextView
android:text="Presets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:id="@+id/txt_head" />

<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#61000000" />
</LinearLayout>

最佳答案

不需要 LayoutInflater。只需使用:

TextView textView = (TextView) findViewById(R.id.txt_head);
texView.setText("Hello");

希望这对您有所帮助。

关于java - 在 MainActivity.java 中更改 content_main.xml 上的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39855500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com