Android进阶(二十七)Android原生扰人烦的布局

举报
SHQ5785 发表于 2020/12/30 00:17:20 2020/12/30
【摘要】 Android原生扰人烦的布局       在开发Android应用时,UI布局是一件令人烦恼的事情。下面主要讲解一下Android中的界面布局。 一、线性布局(LinearLayout)       线性布局分为:       (1)垂直线性布局;       (2)水平线性布局;       针对这两种区别,只是一个属性的区别   <LinearLayou...

Android原生扰人烦的布局

      在开发Android应用时,UI布局是一件令人烦恼的事情。下面主要讲解一下Android中的界面布局。

一、线性布局(LinearLayout)

      线性布局分为:

      (1)垂直线性布局;

      (2)水平线性布局;

      针对这两种区别,只是一个属性的区别

 


  
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. </LinearLayout>

 

      水平线性布局的话,android:orientation="horizontal" 即可。

二、 相对布局(RelativeLayout)

       一般线性布局满足不了们实际项目中的需要,就是一般做Web界面的UI设计一样,也是存在相对元素的一些CSS样式的布局。RelativeLayout参数有:Width,Height,Below,AlignTop,ToLeft,Padding,和MerginLeft。

      关键源码:

 


  
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent">
  4. <TextView
  5. android:id="@+id/label"
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:text="请输入:"/>
  9. <EditText
  10. android:id="@+id/entry"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:background="@android:drawable/editbox_background"
  14. android:layout_below="@id/label"/>
  15. <Button
  16. android:id="@+id/ok"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_below="@id/entry"
  20. android:layout_alignParentRight="true"
  21. android:layout_marginLeft="10dip"
  22. android:text="确定" />
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_toLeftOf="@id/ok"
  27. android:layout_alignTop="@id/ok"
  28. android:text="取消" />
  29. </RelativeLayout>

 

      其中,android:layout_below=”@id/label”设置了EditText处于TextView下方;在Button中android:layout_below=”@id/entry”设置该Button位于EditText下。

      实例效果:

三、表单布局(TableLayout)

      TableLayout由许多TableRow组成,每个TableRow都会定义一个Row。TableLayout容器不会显示Row,Column或Cell的边框线,每个Row拥有0个或多个Cell,每个Cell拥有一个View对象。表格由行和列组成许多单元个,允许单元格为空。但是单元格不能跨列,这与Html不同。

 


  
  1. <View
  2. android:layout_height="2dip"
  3. android:background="#FF909090" />
  4. <TableRow>
  5. <TextView
  6. android:text="*"
  7. android:padding="3dip" />
  8. <TextView
  9. android:text="导入..."
  10. android:padding="3dip" />
  11. </TableRow>
  12. <TableRow>
  13. <TextView
  14. android:text="*"
  15. android:padding="3dip" />
  16. <TextView
  17. android:text="导出..."
  18. android:padding="3dip" />
  19. <TextView
  20. android:text="Ctrl-E"
  21. android:gravity="right"
  22. android:padding="3dip" />
  23. </TableRow>

 

      实例效果:

      android:gravity="center" 书面解释是权重比。其时就是让它居中显示。

      android:stretchColumns="1,2,3,4" 它的意思就是自动拉伸1,2,3,4列。

      若需实现组件居中显示,布局应如下:

 


  
  1. android:layout_width="wrap_content"
  2. android:layout_height="wrap_content"
  3. android:layout_gravity="center_vertical|center_horizontal"

 

      TableRow平分列

      每一列的宽度设置为android:layout_width="0.0dip",在设置每一列的android:layout_weight=“1”

      因为ayout_weight是将剩余空间按权重分配,而不是将全部空间按权重分配。

      代码如下:

 


  
  1. <TableRow
  2. android:paddingTop="15px">
  3. <Button
  4. android:id="@+id/Register"
  5. android:text="@string/register"
  6. android:layout_width="0.0dip"
  7. android:layout_weight="1"
  8. android:onClick="register"/>
  9. <Button
  10. android:id="@+id/cancel"
  11. android:layout_width="0.0dip"
  12. android:layout_weight="1"
  13. android:text="@string/button_cancel" />
  14. </TableRow>

 

      视图如下:

 

四、切换卡(TabWidget)

      切换卡经常用在一下选项上,类似于电话簿界面,通过多个标签切换显示不同内容。而其中,TabHost是一个用来存放Tab标签的容器,通过getTabHost方法来获取TabHost的对象,通过addTab方法向容器里添加Tab。Tab在切换时都会产生一个事件,可以通过TabActivity的事件监听setOnTabChangedListener.

【扩展点】TabHost

类概述

      提供选项卡(Tab页)的窗口视图容器。此对象包含两个子对象:一组是用户可以选择指定Tab页的标签;另一组是FrameLayout用来显示该Tab页的内容。个别元素通常控制使用这个容器对象,而不是设置在子元素本身的值。

(译者madgoat注:即使使用的是单个元素,也最好把它放到容器对象ViewGroup里)

内部类

      interface TabHost.OnTabChangeListener    

      接口定义了当选项卡更改时被调用的回调函数

      interface TabHost.TabContentFactory  

      当某一选项卡被选中时生成选项卡的内容

      class TabHost.TabSpec     

     单独的选项卡,每个选项卡都有一个选项卡指示符,内容和tag标签,以便于记录.。

关键源码

 


  
  1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@android:id/tabhost"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayout
  6. android:orientation="vertical"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent">
  9. <TabWidget
  10. android:id="@android:id/tabs"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content" />
  13. <FrameLayout
  14. android:id="@android:id/tabcontent"
  15. android:layout_width="fill_parent"
  16. android:layout_height="fill_parent">
  17. <TextView
  18. android:id="@+id/textview1"
  19. android:layout_width="fill_parent"
  20. android:layout_height="fill_parent"
  21. android:text="this is a tab" />
  22. <TextView
  23. android:id="@+id/textview2"
  24. android:layout_width="fill_parent"
  25. android:layout_height="fill_parent"
  26. android:text="this is another tab" />
  27. <TextView
  28. android:id="@+id/textview3"
  29. android:layout_width="fill_parent"
  30. android:layout_height="fill_parent"
  31. android:text="this is a third tab" />
  32. </FrameLayout>
  33. </LinearLayout>
  34. </TabHost>

 

     处理类

 


  
  1. //声明TabHost对象
  2. TabHost mTabHost;
  3. public void onCreate(Bundle savedInstanceState)
  4. {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. //取得TabHost对象
  8. mTabHost = getTabHost();
  9. /* 为TabHost添加标签 */
  10. //新建一个newTabSpec(newTabSpec)
  11. //设置其标签和图标(setIndicator)
  12. //设置内容(setContent)
  13. mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
  14. .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))
  15. .setContent(R.id.textview1));
  16. mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
  17. .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))
  18. .setContent(R.id.textview2));
  19. mTabHost.addTab(mTabHost.newTabSpec("tab_test3")
  20. .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))
  21. .setContent(R.id.textview3));
  22. //设置TabHost的背景颜色
  23. mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
  24. //设置TabHost的背景图片资源
  25. //mTabHost.setBackgroundResource(R.drawable.bg0);
  26. //设置当前显示哪一个标签,默认下标从0开始
  27. mTabHost.setCurrentTab(0);
  28. //标签切换事件处理,setOnTabChangedListener
  29. mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
  30. public void onTabChanged(String tabId) {
  31. Dialog dialog = new AlertDialog.Builder(Examples_04_29Activity.this)
  32. .setTitle("提示")
  33. .setMessage("当前选中:"+tabId+"标签")
  34. .setPositiveButton("确定",
  35. new DialogInterface.OnClickListener() {
  36. public void onClick(DialogInterface dialog, int whichButton){
  37. dialog.cancel();
  38. }
  39. }).create();//创建按钮
  40. dialog.show();
  41. }
  42. });
  43. }

 

      实例效果:

 

参考文献

1.http://blog.csdn.net/fancylovejava/article/details/45787729/

美文美图

 

文章来源: shq5785.blog.csdn.net,作者:No Silver Bullet,版权归原作者所有,如需转载,请联系作者。

原文链接:shq5785.blog.csdn.net/article/details/51811816

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。