gpt4 book ai didi

android - 将 View 动态添加到 fragment 内的 ScrollView

转载 作者:行者123 更新时间:2023-11-29 01:43:15 25 4
gpt4 key购买 nike

我有一个必须包含 ScrollView 和按钮的 fragment 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp" >

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<include
android:id="@+id/row1"
android:layout_width="fill_parent"
layout="@layout/catalog_row" />

<View
android:layout_width="match_parent"
android:layout_height="20dip"
android:alpha="0"
android:background="#FFFFFF" />

<include
android:id="@+id/row2"
android:layout_width="fill_parent"
layout="@layout/catalog_row" />

<View
android:layout_width="match_parent"
android:layout_height="20dip"
android:alpha="0"
android:background="#FFFFFF" />

<include
android:id="@+id/row3"
android:layout_width="fill_parent"
layout="@layout/catalog_row" />

<View
android:layout_width="match_parent"
android:layout_height="20dip"
android:alpha="0"
android:background="#FFFFFF" />

<include
android:id="@+id/row4"
android:layout_width="fill_parent"
layout="@layout/catalog_row" />

<View
android:layout_width="match_parent"
android:layout_height="20dip"
android:alpha="0"
android:background="#FFFFFF" />
</LinearLayout>
</ScrollView>

<Button
android:id="@+id/more_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="more"
android:visibility="invisible" />

</RelativeLayout>

现在在某些情况下(当按钮变得可见并且用户单击它时)我会在 scrollView 中添加其他“catalog_row”布局。我怎样才能以编程方式做到这一点?

最佳答案

您必须为 ScrollView 中的 LinearLayout 提供一个 ID,因此在代码中您将:

LinearLayout l = (LinearLayout) findViewById(R.id.yourID);

然后简单地:

l.addView(yourView);

请记住,ScrollView 只能有一个子布局,因此如果您需要另一个布局,则必须设置一个父布局以包含旧布局和新布局。

希望对您有所帮助。

关于android - 将 View 动态添加到 fragment 内的 ScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23104807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com