简体   繁体   English

如何在不使用tabHost的情况下使用按钮更改布局的一部分?

[英]How to change a part of layout with a button without using tabHost?

The main layout of my program has three sections: top, mid and the bottom. 我的程序的主要布局有三个部分:顶部,中部和底部。 By clicking buttons, I want the only mid-section of the screen change with other two parts remained the same. 通过单击按钮,我希望屏幕的唯一中间部分改变,其他两个部分保持不变。 So it's similar with tab layout but without using tabHost I would like to change mid-section screen by imageview. 所以它与标签布局类似,但没有使用tabHost我想通过imageview更改中段屏幕。

I tried to start a new activity contained in a view, but failed. 我尝试启动视图中包含的新活动,但失败了。 Also tried to load entirely new layout containing all three sections, but also failed due to memory issues. 还尝试加载包含所有三个部分的全新布局,但也因内存问题而失败。

Thanks for advance! 谢谢你提前!

The proper way to handle this requirement will be to use fragments. 处理此要求的正确方法是使用片段。 It can be used by including the compatibility library of android which is jar include in the android sdk folder. 它可以通过包含android的兼容性库来使用,这是jar包含在android sdk文件夹中。 Here is a tutorial . 这是一个教程

If it is something simple like switch between simple views. 如果它像简单视图之间的切换一样简单。 You can also use a view flipper. 您还可以使用视图鳍状肢。

This is very simple 这很简单

First Method: 第一种方法:

step 1: design common xml layout for top,mid,bottom.

step 2: every Activity you have to inflate or change the mid part .

    setContentView(R.layout.common_xml_layout);
        mid= (LinearLayout) findViewById(R.id.mid_view);
        mid.removeAllViews();
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.Your_Inflate_Xml, null);
        mid.addView(v);

In First Method you have to take care of finish the Activities. 在First Method中,您必须完成活动。

Second Method : 第二种方法:

step 1: design common xml layout for top,mid,bottom.

step 2: Extends your MainActivity 


Your MainActivity-->public class MainActivity extends Activity 

Your subActivities-->public class subActivity extends MainActivity

You should use fragments to achieve this. 您应该使用片段来实现此目的。 Use android.app.Fragment for v11 or later and use the compatibility library Fragment for versions < 11. 对于v11或更高版本使用android.app.Fragment,并对版本<11使用兼容性库Fragment。

Here is a tutorial on how fragments could be used in a modified version of Notepad app Honeypad Tutorial 这是一个关于如何在修改版的Notepad app Honeypad Tutorial中使用片段的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM