在膨胀布局中使用 setText() 到 TextView() 后不显示文本
text isn't showing after using setText() to TextView() in inflated layout
是的,我知道以前有人问过类似的问题,但我尝试了很多。所以我在膨胀布局的 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>
感谢您的回答!
感谢评论!问题已解决!
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);
}
是的,我知道以前有人问过类似的问题,但我尝试了很多。所以我在膨胀布局的 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>
感谢您的回答!
感谢评论!问题已解决!
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);
}