gpt4 book ai didi

android - 如何使用 layout inflatter 在另一个 View 中添加一个 View

转载 作者:行者123 更新时间:2023-11-30 04:07:17 24 4
gpt4 key购买 nike

我用两个按钮创建了 mainLayout

add:添加其他布局

remove : 移除其他布局。

    <Button
android:id="@+id/btnAdd"
android:textStyle="bold"

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add View"
android:onClick="addView" />

<Button
android:id="@+id/btnRemove"
android:textStyle="bold"

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove View"
android:onClick="removeView" />

现在我写了下面的代码来添加 View 当我点击 addView 按钮时

               LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.other_layout,null);


mainLayout.addView(view);

View 添加到主布局下方。但我希望 View 在 addView 按钮下方添加(在 removeView 按钮上方而不是在主布局的底部)

我该怎么做?

最佳答案

在两个按钮之间添加一个框架布局。

然后在运行时将您的 View 膨胀到框架布局中。

LayoutInflater inflater= (LayoutInflater)this.getSystemService (LAYOUT_INFLATER_SERVICE);     view=inflater.inflate(R.layout.other_layout,null);  
myframeLayout.addView(view);

关于android - 如何使用 layout inflatter 在另一个 View 中添加一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11407703/

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