text isn't showing after using setText() to TextView() in inflated layout

流过昼夜 提交于 2021-02-10 13:59:29

问题


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));
}

And here is 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>

Thank you for answer!


回答1:


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);
}


来源:https://stackoverflow.com/questions/47541214/text-isnt-showing-after-using-settext-to-textview-in-inflated-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!