gpt4 book ai didi

android - 如何以编程方式将布局添加到当前布局的末尾?

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

看起来这应该很容易,但到目前为止运气不好。我设置了 Activity 的主布局,然后根据单击哪个按钮,我需要将特定布局添加到当前主布局的末尾。

主要 Activity :

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//live_video_layout is a vertical LinearLayout
setContentView(R.layout.live_video_layout);

//Psuedo code for clarity; these layouts are ScrollViews that will sit under live_video_layout seen above
if(button1 is clicked)
{add R.id.first_layout under live_video_layout}

else if(button2 is clicked)
{add R.id.second_layout under live_video_layout
}

这里的任何指导将不胜感激,我完全没有想法。

最佳答案

您可以在 xml 文件中准备两个布局,第一个用于视频观看,第二个用于动态布局:

main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/live_video_container">

<!-- include your live_video_layout.xml or directly add your views-->

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dynamic_layouts_container">
<!-- here we'll add layouts from java code depending on buttons click -->
</LinearLayout>
</LinearLayout>

然后在你的 Activity 中:

    private LinearLayout mDynamicLayoutsContainer;
private LayoutInflater mInflater;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main_activity);

mInflater = LayoutInflater.from(context);
mDynamicLayoutsContainer = (LinearLayout) findViewById(R.id.dynamic_layouts_container);

if(button1 is clicked){

View firstLayout = mInflater.inflate(R.layout.first_layout , null, false);
mDynamicLayoutsContainer.addView(firstLayout);

}else if(button2 is clicked){

View secondLayout = mInflater.inflate(R.layout.second_layout , null, false);
mDynamicLayoutsContainer.addView(secondLayout);

}
}

关于android - 如何以编程方式将布局添加到当前布局的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32852138/

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