TextView setText 不工作
TextView setText not working
我正在尝试将 setText 设置为 activity 中包含的片段中的文本视图。当我使用 setText 方法设置文本时,我可以看到为 textview 的 mText class 变量分配了该值。但是,在 UI 端我看不到代码设置的文本。
我有这样的 activity 布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/meaningcontainer"
tools:context="com.xyz.MeaningActivity"
tools:ignore="MergeRootFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/meaning_toolbar"
android:id="@+id/word_selected"
android:textSize="@dimen/abc_text_size_title_material_toolbar"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/word_selected"
android:id="@+id/word_meanings"
>
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frag_meaning"
android:name="com.xyz.ui.MeaningActivity$MeaningFragment"
tools:layout="@layout/fragment_meaning"
/>
</FrameLayout>
此布局中包含的相应片段如下:
<ScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.MeaningActivity$MeaningFragment"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="@dimen/cardview_default_elevation"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:id="@+id/postype_noun"
android:textAlignment="textStart"
android:textSize="@dimen/abc_text_size_title_material_toolbar"
android:layout_marginStart="@dimen/activity_horizontal_margin"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
在 activity onCreate 方法中,我尝试设置文本。这是相关代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meaning);
if (savedInstanceState == null) {
Fragment fragment = new MeaningFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.word_meanings, fragment).commit();
}
Fragment fragment = getSupportFragmentManager().
findFragmentById(R.id.frag_meaning);
View frag_view = fragment.getView();
TextView postextView = (TextView) frag_view.
findViewById(R.id.postype_noun);
postextView.setText("test");
}
textView posttextView 不为 null 并被赋予 test 的值。但是在 UI 端我看不到任何测试字符串。
请从您的 activity 的 onCreate 方法中删除以下行:
if (savedInstanceState == null) {
Fragment fragment = new MeaningFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.word_meanings, fragment).commit();
}
无需执行此操作,因为您的片段已通过 activity 布局中的 XML 实例化。
尝试从您的 activity 布局中删除卡片布局。
我正在尝试将 setText 设置为 activity 中包含的片段中的文本视图。当我使用 setText 方法设置文本时,我可以看到为 textview 的 mText class 变量分配了该值。但是,在 UI 端我看不到代码设置的文本。
我有这样的 activity 布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/meaningcontainer"
tools:context="com.xyz.MeaningActivity"
tools:ignore="MergeRootFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/meaning_toolbar"
android:id="@+id/word_selected"
android:textSize="@dimen/abc_text_size_title_material_toolbar"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/word_selected"
android:id="@+id/word_meanings"
>
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frag_meaning"
android:name="com.xyz.ui.MeaningActivity$MeaningFragment"
tools:layout="@layout/fragment_meaning"
/>
</FrameLayout>
此布局中包含的相应片段如下:
<ScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.MeaningActivity$MeaningFragment"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="@dimen/cardview_default_elevation"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:id="@+id/postype_noun"
android:textAlignment="textStart"
android:textSize="@dimen/abc_text_size_title_material_toolbar"
android:layout_marginStart="@dimen/activity_horizontal_margin"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
在 activity onCreate 方法中,我尝试设置文本。这是相关代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meaning);
if (savedInstanceState == null) {
Fragment fragment = new MeaningFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.word_meanings, fragment).commit();
}
Fragment fragment = getSupportFragmentManager().
findFragmentById(R.id.frag_meaning);
View frag_view = fragment.getView();
TextView postextView = (TextView) frag_view.
findViewById(R.id.postype_noun);
postextView.setText("test");
}
textView posttextView 不为 null 并被赋予 test 的值。但是在 UI 端我看不到任何测试字符串。
请从您的 activity 的 onCreate 方法中删除以下行:
if (savedInstanceState == null) {
Fragment fragment = new MeaningFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.word_meanings, fragment).commit();
}
无需执行此操作,因为您的片段已通过 activity 布局中的 XML 实例化。
尝试从您的 activity 布局中删除卡片布局。