更改MainActivity.java中content_main.xml的文本视图
我使用的是Android Studio标准模板“导航抽屉活动”,其中包含content_main.xml布局 在这个布局上是我的文本视图,我想更改MainActivity.java中的文本 我尝试了以下内容,但没有任何变化:更改MainActivity.java中content_main.xml的文本视图,java,android,xml,Java,Android,Xml,我使用的是Android Studio标准模板“导航抽屉活动”,其中包含content_main.xml布局 在这个布局上是我的文本视图,我想更改MainActivity.java中的文本 我尝试了以下内容,但没有任何变化: LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE); View view = inflater.infla
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");
<?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>
content\u main.xml:
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");
<?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>
不需要更平坦的布局。只需使用:
TextView textView = (TextView) findViewById(R.id.txt_head);
texView.setText("Hello");
希望这能有所帮助。您只需以正常方式访问TextView组件即可。使用该模板,content_main.xml包含在主布局中,因此您应该在主要活动中执行以下操作(可能在onCreate方法中或任何需要的地方): 假设在包含内容的主布局中有一个app_bar_main.xml。最终,所有内容都可以直接从主活动访问,因为布局包含在主布局中
希望有帮助 哇,我真是瞎了哦,谢谢你!不用担心……)谢谢你,我真是瞎了!