簡體   English   中英

TextView setText不起作用

[英]TextView setText not working

我正在嘗試將setText設置為活動中包含的片段中的textviews。 當我使用setText方法設置文本時,可以看到為textview的mText類變量分配了該值。 但是,在UI端,我看不到代碼設置的文本。

我有這樣的活動布局。

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

在活動onCreate方法中,我嘗試設置setText。 這是該代碼:

@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 postextView不為null,並為其分配test的值。 但是在UI端,我看不到任何測試字符串。

請從活動的onCreate方法中刪除以下行:

if (savedInstanceState == null) {
    Fragment fragment = new MeaningFragment();
    getSupportFragmentManager().beginTransaction()
            .add(R.id.word_meanings, fragment).commit();
}

不需要這樣做,因為您的片段已經通過XML在活動布局中實例化了。

嘗試從活動布局中刪除卡片布局。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM