在膨胀布局中使用setText()到TextView()后,文本未显示 [英] text isn't showing after using setText() to TextView() in inflated layout

查看:74
本文介绍了在膨胀布局中使用setText()到TextView()后,文本未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道之前也曾问过类似的问题,但我尝试了许多类似的问题.所以我有一个问题,在膨胀布局的TextView中设置文本.我尝试了setContentView()然后它可以工作,但随后带有菜单的activity_main.xml无法工作.因此,我尝试使用所需的TextView来增加布局.当我尝试setText()时,它只是不显示它.这是我的代码:

Yes, I know that similar questions were asked before but I tried many of those. So I have a problem with setting text in TextView in the inflated layout. I tried setContentView() then it works but then activity_main.xml with the menu isn't working. So I tried to inflate layout with TextView that I need. When I try to setText() it just doesn't displaying it. Here is my code:

public void bodAlg() {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vi = inflater.inflate(R.layout.layout1,  null);  

    EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
    String[] serversArray = editText.getText().toString().split(", ");

    TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
    textView.setText(Arrays.toString(serversArray));
}

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:id="@+id/rl_01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/ll_01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/bod_servers"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Cray CS-Storm, Vulcan – Blue Gene/Q, Blue Gene/Q, Stampede – PowerEdge C8220, Piz Daint – Cray XC30"
            android:textColor="@color/common_google_signin_btn_text_dark_focused"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/bod_serv_array"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="15sp"
            android:textColor="@color/common_google_signin_btn_text_dark_focused"
            android:paddingBottom="5dp"/>
    </LinearLayout>
</RelativeLayout>
</ScrollView>

谢谢您的回答!

推荐答案

感谢您的评论!问题解决了!

Thanks for comments! Problem is solved!

public void bodAlg() {
    LayoutInflater inflater = (LayoutInflater) 
    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vi = inflater.inflate(R.layout.layout1,  null);  

    EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
    String[] serversArray = editText.getText().toString().split(", ");

    TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
    textView.setText(Arrays.toString(serversArray));

    // solution 
    CoordinatorLayout mainL = (CoordinatorLayout) findViewById(R.id.main_view);
    mainL.removeAllViews(); // remove previous view, add 2nd layout
    mainL.addView(vi);
}

这篇关于在膨胀布局中使用setText()到TextView()后,文本未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆