Sha*_*hah 5 android view dynamic android-linearlayout
我有一个具有以下结构的abc.xml.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我想动态地将textviews添加到线性布局.以下是我的代码.我没有得到任何错误,但我没有得到预期的结果.
LayoutInflater Inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = Inflater.inflate(R.layout.abc, null);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear);
TextView Tag = new TextView(getActivity());
Tag.setText("textString");
Tag.setBackgroundResource(R.color.bg_color);
Tag.setTextAppearance(getActivity(), R.style.SmallFont);
layout.addView(Tag);
Run Code Online (Sandbox Code Playgroud)
你的xml应该是
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
和添加文本视图的java代码是
LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
TextView Tag = new TextView(getActivity());
tag.setLayoutParams(params);
Tag.setText("textString");
Tag.setBackgroundResource(R.color.bg_color);
Tag.setTextAppearance(getActivity(), R.style.SmallFont);
layout.addView(Tag);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11828 次 |
| 最近记录: |