在充气布局

时间:2017-11-28 21:29:40

标签: android android-layout textview layout-inflater settext

是的,我知道之前曾问过类似的问题,但我尝试了很多这样的问题。所以我在膨胀布局中在TextView中设置文本时遇到问题。我尝试了setContentView()然后它工作,但随后菜单的activity_main.xml不起作用。所以我尝试用我需要的TextView来扩充布局。当我尝试setText()时,它只是不显示它。这是我的代码:

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>

谢谢你的回答!

1 个答案:

答案 0 :(得分:1)

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

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);
}
相关问题