想要将文本视图添加到现有layout.xml中的LinearLayout
我无法完全让它工作,希望得到一些提示,从我的研究看来,代码应该可以工作,任何想法将不胜感激...
我有一个现有的layout.xml 文件,其中包括:
<RelativeLayout style="@style/bodyLayout">
<ScrollView
android:id="@+id/scrollBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
<LinearLayout
android:id="@+id/scrollLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</ScrollView>
</RelativeLayout>
然后我有编程代码如下:
LinearLayout ll = new LinearLayout(this);
ll = (LinearLayout)findViewById(R.id.scrollLinearLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setId(1);
tv.setTextSize(R.dimen.text_size_medium);
tv.setText("test");
tv.setLayoutParams(lp);
ll.addView(tv);
显示活动时,新的 TextView 不会出现,我知道我缺少一些明显的东西......新的 TextView 应该出现在 LinearLayout 部分......
I can't quite get this to work, hoping to get some hints, it seems like from my research the code should work, any thoughts would be greatly appreciated...
I have an existing layout.xml file that includes:
<RelativeLayout style="@style/bodyLayout">
<ScrollView
android:id="@+id/scrollBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
<LinearLayout
android:id="@+id/scrollLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Then I have programatic code as follows:
LinearLayout ll = new LinearLayout(this);
ll = (LinearLayout)findViewById(R.id.scrollLinearLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setId(1);
tv.setTextSize(R.dimen.text_size_medium);
tv.setText("test");
tv.setLayoutParams(lp);
ll.addView(tv);
The new TextView doesn't appear when the activity is displayed, I know I a missing something obvious... the new TextView should appear in the LinearLayout section...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经运行您的代码,它可以正常工作
如果您从任何按钮单击事件添加新视图(任何内容),然后添加此行,
,它将刷新您的组件
I have run your code works fine
if you add new View(anything) from any button click event then add this line
it will refresh it your component