如何以编程方式将布局添加到当前布局的末尾?
How to programmatically add a layout to end of the current layout?
看起来这应该很容易,但到目前为止运气不好。我设置了 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
}
这里的任何指导将不胜感激,我完全没有想法。
您需要第三个布局作为父布局。然后你把 R.layout.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);
}
}
看起来这应该很容易,但到目前为止运气不好。我设置了 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
}
这里的任何指导将不胜感激,我完全没有想法。
您需要第三个布局作为父布局。然后你把 R.layout.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);
}
}