最近我问了一个关于如何为 TextView 设置一种样式并为其他文本多次显示它的问题。解决方案是创建一个 XML 布局,我在其中设计 textview,然后使用 ArrayAdapter 填充内容。我在片段中使用 ArrayAdapter,因为我有多个片段替换依赖于菜单单击的主片段。
但我被困住了。我真的不知道如何实现这一目标。我将所有值写入一个数组,然后将它们分配给我的 ArrayAdapter。我见过很多解决方案,但没有一个能解决我的问题,他们都使用了另一种方式。
这是片段的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ScrollView
android:id="@+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.03" >
<TextView
android:id="@+id/sources"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
</ScrollView>
</RelativeLayout>
TextView 是我要填充的文本视图。目前还没有样式,我这样做只是为了测试目的。现在我在片段中做到了这一点。
这是我的 onCreateView 方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
StrictMode.setThreadPolicy(policy);
View rootView = inflater.inflate(R.layout.main_fragment, null);
RelativeLayout ll = new RelativeLayout(getActivity());
ScrollView sv = new ScrollView(getActivity());
ArrayAdapter<String> sourceAdapter = new ArrayAdapter<String>(getActivity(), R.id.mainScrollView, R.id.sources, sourceArr);
return rootView;
}
sourceArr
是包含内容的数组。如何将所有值分配给 TextView 以便多次显示?谢谢