android – 动态地将布局添加到Scrollview中

weixin_38066537 2019-09-12 12:34:40
这个问题似乎已经被问了十几次我已经无法从他们那里推断出解决方案了. 我有一个名为tile.xml的布局,如下所示: <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" android:text="May Fong Robinson" android:textAppearance="?android:attr/textAppearanceLarge" android:textStyle="bold" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia" android:textAppearance="?android:attr/textAppearanceMedium" /> 我需要添加我的一个名为feed.xml的视图,我已将其声明为ScrollView,因为我想在其中包含多个对象,因此我可以垂直滚动它们.这是我的feed.xml文件: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="fill_parent" android:background="#F1F1F1" android:layout_height="fill_parent" > </ScrollView> 我想动态地将我的“tile”从tile.xml添加到我的“feed”feed.xml视图,同时动态更改TextViews的文本. 我怎样才能做到这一点?谢谢 新的feed.xml: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#F1F1F1" > <LinearLayout android:id="@+id/tiles" android:layout_width="match_parent" android:layout_height="wrap_content" > </LinearLayout> </ScrollView> …和新的tile.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" android:background="#FFFFFF" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" android:text="May Fong Robinson" android:textAppearance="?android:attr/textAppearanceLarge" android:textStyle="bold" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
...全文
138 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38070905 2019-09-12
  • 打赏
  • 举报
回复
首先,在布局中添加2个TextView. 然后,在您的代码中,您要动态添加TextViews,执行以下操作: ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1); LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.tile, null); scroll.addView(view); 要访问TextView,您必须执行以下操作: TextView textView1 = (TextView) view.findViewById(R.id.textView1); textView1.setText(""); 还要确保在XML文件中为TextView提供不同的ID.目前它们都是textView1

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧