首页 > 系统 > Android > 正文

android编程实现局部界面动态切换的方法

2019-10-24 20:34:49
字体:
来源:转载
供稿:网友

这篇文章主要介绍了android编程实现局部界面动态切换的方法,以实例形式较为详细的分析了Android局部切换的布局及功能实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了android编程实现局部界面动态切换的方法。分享给大家供大家参考,具体如下:

局部界面固定,局部界面可以动态切换。效果如下:

android编程实现局部界面动态切换的方法

android编程实现局部界面动态切换的方法

android编程实现局部界面动态切换的方法

这个效果由3个layout构成

main.xml

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent" 
  5. android:orientation="horizontal" > 
  6. <LinearLayout 
  7. android:layout_width="fill_parent" 
  8. android:layout_height="fill_parent" 
  9. android:layout_weight="1" 
  10. android:background="@android:color/black" > 
  11. <Button 
  12. android:id="@+id/btnSwitch" 
  13. android:layout_width="wrap_content" 
  14. android:layout_height="wrap_content" 
  15. android:text="switch" /> 
  16. <Button 
  17. android:id="@+id/btnScreen" 
  18. android:layout_width="wrap_content" 
  19. android:layout_height="wrap_content" 
  20. android:text="screen" /> 
  21. </LinearLayout> 
  22. <LinearLayout 
  23. android:id="@+id/frameSwitch" 
  24. android:layout_width="160dp" 
  25. android:layout_height="fill_parent" 
  26. android:background="@android:color/white" > 
  27. </LinearLayout> 
  28. </LinearLayout> 

one.xml

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent" 
  5. android:background="@color/yellow" 
  6. android:orientation="vertical" > 
  7. <TextView 
  8. android:layout_width="wrap_content" 
  9. android:layout_height="wrap_content" 
  10. android:text="this is linearLayout one" /> 
  11. </LinearLayout> 

two.xml

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:layout_width="match_parent" 
  4. android:layout_height="match_parent" 
  5. android:orientation="vertical" > 
  6. <TextView 
  7. android:layout_width="wrap_content" 
  8. android:layout_height="wrap_content" 
  9. android:text="this is linearLayout two" /> 
  10. <Button 
  11. android:id="@+id/btnSecond" 
  12. android:layout_width="wrap_content" 
  13. android:layout_height="wrap_content" 
  14. android:text="btnSecond" /> 
  15. </LinearLayout> 

下面是Java代码

 

 
  1. public class ZzzAndroidActivity extends Activity { 
  2. private LinearLayout frameSwitch; 
  3. /** Called when the activity is first created. */ 
  4. @Override 
  5. public void onCreate(Bundle savedInstanceState) { 
  6. super.onCreate(savedInstanceState); 
  7. setContentView(R.layout.main); 
  8. frameSwitch = (LinearLayout) findViewById(R.id.frameSwitch); 
  9. Button btnSwitch = (Button) findViewById(R.id.btnSwitch); 
  10. btnSwitch.setOnClickListener(new OnClickListener() { 
  11. boolean boo = false
  12. @Override 
  13. public void onClick(View v) { 
  14. boo = !boo; 
  15. if (boo) { 
  16. getViewOne(); 
  17. else { 
  18. getViewSecond(); 
  19. }); 
  20. /* 
  21. * 是否全屏 
  22. */ 
  23. Button btnScreen = (Button) findViewById(R.id.btnScreen); 
  24. btnScreen.setOnClickListener(new OnClickListener() { 
  25. boolean isScreen = false
  26. @Override 
  27. public void onClick(View v) { 
  28. isScreen = !isScreen; 
  29. if (isScreen) { 
  30. frameSwitch.setVisibility(android.view.View.GONE); 
  31. else { 
  32. frameSwitch.setVisibility(android.view.View.VISIBLE); 
  33. }); 
  34. public void getViewOne() { 
  35. View viewOne = getLayoutInflater().inflate(R.layout.one, null); 
  36. frameSwitch.removeAllViews(); 
  37. frameSwitch.addView(viewOne, LayoutParams.FILL_PARENT, 
  38. LayoutParams.FILL_PARENT); 
  39. public void getViewSecond() { 
  40. View viewSecond = getLayoutInflater().inflate(R.layout.two, null); 
  41. Button btn = (Button) viewSecond.findViewById(R.id.btnSecond); 
  42. btn.setOnClickListener(new OnClickListener() { 
  43. @Override 
  44. public void onClick(View v) { 
  45. Toast.makeText(ZzzAndroidActivity.this"hello world"
  46. Toast.LENGTH_LONG).show(); 
  47. }); 
  48. frameSwitch.removeAllViews(); 
  49. frameSwitch.addView(viewSecond, LayoutParams.FILL_PARENT, 
  50. LayoutParams.FILL_PARENT); 

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表