`
java-mans
  • 浏览: 11423300 次
文章分类
社区版块
存档分类
最新评论

Android android:gravity属性介绍及效果图

 
阅读更多

android:gravity的属性官方说明如下:

public static final int AXIS_CLIP

Since: API Level 3
Raw bit controlling whether the right/bottom edge is clipped to its container, based on the gravity direction being applied.
Constant Value: 8 (0x00000008)

public static final int AXIS_PULL_AFTER

Since: API Level 1
Raw bit controlling how the right/bottom edge is placed.
Constant Value: 4 (0x00000004)

public static final int AXIS_PULL_BEFORE

Since: API Level 1
Raw bit controlling how the left/top edge is placed.
Constant Value: 2 (0x00000002)

public static final int AXIS_SPECIFIED

Since: API Level 1
Raw bit indicating the gravity for an axis has been specified.
Constant Value: 1 (0x00000001)

public static final int AXIS_X_SHIFT

Since: API Level 1
Bits defining the horizontal axis.
Constant Value: 0 (0x00000000)

public static final int AXIS_Y_SHIFT

Since: API Level 1
Bits defining the vertical axis.
Constant Value: 4 (0x00000004)

public static final int BOTTOM

Since: API Level 1
Push object to the bottom of its container, not changing its size.
Constant Value: 80 (0x00000050)

public static final int CENTER

Since: API Level 1
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
Constant Value: 17 (0x00000011)

public static final int CENTER_HORIZONTAL

Since: API Level 1
Place object in the horizontal center of its container, not changing its size.
Constant Value: 1 (0x00000001)

public static final int CENTER_VERTICAL

Since: API Level 1
Place object in the vertical center of its container, not changing its size.
Constant Value: 16 (0x00000010)

public static final int CLIP_HORIZONTAL

Since: API Level 3
Flag to clip the edges of the object to its container along the horizontal axis.
Constant Value: 8 (0x00000008)

public static final int CLIP_VERTICAL

Since: API Level 3
Flag to clip the edges of the object to its container along the vertical axis.
Constant Value: 128 (0x00000080)

public static final int DISPLAY_CLIP_HORIZONTAL

Since: API Level 3
Special constant to enable clipping to an overall display along the horizontal dimension. This is not applied by default by apply(int, int, int, Rect, int, int, Rect); you must do so yourself by calling applyDisplay(int, Rect, Rect).
Constant Value: 16777216 (0x01000000)

public static final int DISPLAY_CLIP_VERTICAL

Since: API Level 3
Special constant to enable clipping to an overall display along the vertical dimension. This is not applied by default by apply(int, int, int, Rect, int, int, Rect); you must do so yourself by calling applyDisplay(int, Rect, Rect).
Constant Value: 268435456 (0x10000000)

public static final int END

Since: API Level 14
Push object to x-axis position at the end of its container, not changing its size.
Constant Value: 8388613 (0x00800005)

public static final int FILL

Since: API Level 1
Grow the horizontal and vertical size of the object if needed so it completely fills its container.
Constant Value: 119 (0x00000077)

public static final int FILL_HORIZONTAL

Since: API Level 1
Grow the horizontal size of the object if needed so it completely fills its container.
Constant Value: 7 (0x00000007)

public static final int FILL_VERTICAL

Since: API Level 1
Grow the vertical size of the object if needed so it completely fills its container.
Constant Value: 112 (0x00000070)

public static final int HORIZONTAL_GRAVITY_MASK

Since: API Level 1
Binary mask to get the absolute horizontal gravity of a gravity.
Constant Value: 7 (0x00000007)

public static final int LEFT

Since: API Level 1
Push object to the left of its container, not changing its size.
Constant Value: 3 (0x00000003)

public static final int NO_GRAVITY

Since: API Level 1
Constant indicating that no gravity has been set
Constant Value: 0 (0x00000000)

public static final int RELATIVE_HORIZONTAL_GRAVITY_MASK

Since: API Level 14
Binary mask for the horizontal gravity and script specific direction bit.
Constant Value: 8388615 (0x00800007)

public static final int RELATIVE_LAYOUT_DIRECTION

Since: API Level 14
Raw bit controlling whether the layout direction is relative or not (START/END instead of absolute LEFT/RIGHT).
Constant Value: 8388608 (0x00800000)

public static final int RIGHT

Since: API Level 1
Push object to the right of its container, not changing its size.
Constant Value: 5 (0x00000005)

public static final int START

Since: API Level 14
Push object to x-axis position at the start of its container, not changing its size.
Constant Value: 8388611 (0x00800003)

public static final int TOP

Since: API Level 1
Push object to the top of its container, not changing its size.
Constant Value: 48 (0x00000030)

public static final int VERTICAL_GRAVITY_MASK

Since: API Level 1
Binary mask to get the vertical gravity of a gravity.
Constant Value: 112 (0x00000070)
效果图1:
布局文件xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   
   <TextView android:id="@+id/TextView01"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="top"
   		android:gravity="top"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView02"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="bottom"
   		android:gravity="bottom"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView03"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="left"
   		android:gravity="left"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView04"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="right"
   		android:gravity="right"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView05"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="center_vertical"
   		android:gravity="center_vertical"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView06"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="fill_vertical"
   		android:gravity="fill_vertical"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView07"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="center_horizontal"
   		android:gravity="center_horizontal"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView08"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="fill_horizontal"
   		android:gravity="fill_horizontal"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>   		   		   		   		   		   		   		

   <TextView android:id="@+id/TextView09"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="center"
   		android:gravity="center"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView10"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="fill"
   		android:gravity="fill"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView11"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="clip_vertical"
   		android:gravity="clip_vertical"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView12"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="clip_horizontal"
   		android:gravity="clip_horizontal"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		   		   		   		   		
</LinearLayout>
效果图2:
xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   
   <TextView android:id="@+id/TextView01"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="top"
   		android:gravity="top"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView02"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="bottom"
   		android:gravity="bottom"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView03"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="left"
   		android:gravity="left"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView04"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="right"
   		android:gravity="right"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView05"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="center_vertical"
   		android:gravity="center_vertical"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView06"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="fill_vertical"
   		android:gravity="fill_vertical"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView07"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="center_horizontal"
   		android:gravity="center_horizontal"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView08"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="fill_horizontal"
   		android:gravity="fill_horizontal"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>   		   		   		   		   		   		   		

   <TextView android:id="@+id/TextView09"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="center"
   		android:gravity="center"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView10"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="fill"
   		android:gravity="fill"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView11"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="clip_vertical"
   		android:gravity="clip_vertical"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView12"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="clip_horizontal"
   		android:gravity="clip_horizontal"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		   		   		   		   		
</LinearLayout>


分享到:
评论

相关推荐

    Android高薪之路:Android程序员面试宝典 李宁

    2 3 3 android:layout gravity属性和android:gravity属性 2 4 高级布局技术 第3章 组件 3 1 组件的属性 3 2 文本组件 3 3 按钮组件 3 4 图像组件 3 5 进度组件 3 6 列表组件 3 7 容器组件 3 8 自定义组件 3 9 四大...

    Android开发EditText属性.txt

    这里测试为一个汉字字符宽度,如图: android:maxEms设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。 android:minEms设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems...

    Android上拉面板AndroidSlidingUpPanel.zip

    效果图: 用法:使用com.sothree.slidinguppanel.SlidingUpPanelLayout作为您的活动布局的根元素。 布局必须设置为顶部或底部。请确保它有两个元素。 第一个元素是你的主要布局。第二个元素是你的向上...

    Android布局之LinearLayout线性布局

    先给大家展示一下导图: 知识点详解(演示效果方便组件没有设置id) (1)gravity和Layout_gravity android:gravity 属性是对该view中内容的限定.比如一个button 上面的text. 你可以设置该text 相对于view的靠...

    Parallax Everywhere-使ImageView和TextView产生视差效果.zip

    项目地址:https://github.com/Narfss/ParallaxEverywhere 效果图:如何使用:&lt;FrameLayout  android:layout_width="0dp"  android:layout_height="match_parent"  android:layout_gravity="center"  ...

    wkp111_StickLayout-粘性控件,其任意一个子控件都可滑动停留,本质为NestedScrollView和LinearLayout的结合。.zip

    演示图 Note:图1为设置属性wkp_canScrollToEndViewTop=true,图2没有;图3为设置滑动改变监听。Gradle集成dependencies{  compile 'com.wkp:StickLayout:1.0.6'  //Android Studio3.0 可用以下方式  //...

    Masaccio-一个通过检测图像中人脸所在位置,剪裁出最佳效果图片的ImageView.zip

    项目地址:https://github.com/Subito-it/Masaccio 效果图:如何使用MasaccioImageView继承自android.widget.ImageView, 所以使用上和ImageView是一样的。  android:id="@ id/masaccio_view"  android:layout_...

    Android实现水波纹特效

    最近需要做个类似于水波纹动画的效果,思考了一下不需要UI切个动态图,Android原生的技术利用动画或者自定义控件都可以实现,下面上个图类似于这样的效果 下面请看第一种动画实现,这种方式较为简单些,就是利用3个...

    高仿微信界面

    android:gravity="center" android:orientation="vertical"&gt; android:id="@+id/bt_page_more" android:layout_width="35dp" android:layout_height="35dp" android:layout_gravity="center_horizontal" ...

    Squint-为图片设置斜切效果的组件.zip

    可以为图片添加斜切效果的效果:添加依赖allprojects {  repositories { ...  maven { url "https://jitpack.io" }  }  }dependencies {  compile 'com.github.IntruderShanky:Squint:3.0.1'  ...

    星座说明书

    android:gravity="center_horizontal" android:layout_below="@id/textView2" android:layout_centerHorizontal="true" android:text="确认" android:textSize="20px" &gt; &lt;TextView android:id="@+id/...

    Android TextView(圆弧)边框和背景实例详解

    效果图: 布局代码: &lt;TextView android:id=@+id/product_tag android:layout_width=wrap_content android:layout_height=wrap_content android:gravity=center android:singleLine=true android:...

    ExtendedTouchView-在不改变触发对象的情况下,增加点击区域的控件.zip

    项目地址:https://github.com/lnikkila/ExtendedTouchView 效果图:如何使用使用很简单,就是把想要增加区域的view的外面加个&lt;com.lnikkila.extendedtouchview.ExtendedTouchView&gt;:  android:id="@ id/touch_view...

    banner:android 轮播图控件

    BannerViewPager轮播图控件预览1.用法Android Studio使用依赖:compile 'cn.kevin:bannerview:1.0.8'或者下载工程后使用compile project(':banner')2.功能实现广告轮播,通过设置参数,可以实现当前item、前一个、后...

    Android自定义实现开关按钮代码

    个人认为还是自定义的比较好,先上个效果图: 实现过程: 1.准备开关不同状态的两张图片放入drawable中。 2.xml文件中添加代码: &lt;ToggleButton android:id=@+id/switch1 android:layout_width=wrap_...

    TimeBar-一个仿照印象笔记的TimeBar.zip

    项目地址:https://github.com/wuapnjie/TimeBar 效果图:如何使用: android:id="@ id/timeBar" android:layout_gravity="center" app:time_textSize="16sp" app:time_textColor_day="@color/time_bar_...

    DragTopLayout-一个在ViewPager头部添加一个可以下拉的头部用以显示说明信息。.zip

    项目地址:https://github.com/chenupt/DragTopLayout 效果图:如何使用在布局中添加DragTopLayout  android:layout_width="match_parent"  android:layout_height="match_parent"&gt;    &lt;!--top view--&...

    Android根据输入银行卡号判断属于哪个银行

    一:一般都是先来效果图: 二:实现步骤: 1.xml布局实现,两个edittext就行了 &lt;LinearLayout android:id=@+id/lin_yhkh android:layout_width=fill_parent android:layout_height=48dp android:layout_below=@...

Global site tag (gtag.js) - Google Analytics