首页 > 系统 > Android > 正文

Android 控件(button)对齐方法实现详解

2019-10-24 20:58:35
字体:
来源:转载
供稿:网友
horizontal是让所有的子元素按水平方向从左到右排列,vertical是让所有的子元素按竖直方向从上到下排列,下面为大家介绍下控件(button)的对齐方法
 
1,android:orientation 
布局方向。horizontal是让所有的子元素按水平方向从左到右排列, vertical是让所有的子元素按竖直方向从上到下排列。 

2,android:gravity 与 android:layout_gravity的区别android:gravity是指定本元素的子元素相对它的对齐方式。 

android:layout_gravity是指定本元素相对它的父元素的对齐方式。 
例如: 
下面这里的linearlayout的android:gravity设为right,有两个子元素Button01和Button02。 
java代码: 
复制代码代码如下:

<?xml version=”1.0″ encoding=”utf-8″?> 
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” 
android:orientation=”vertical” 
android:layout_width=”fill_parent” 
android:layout_height=”fill_parent” 
android:gravity=”right” 

<Button android:text=”button01″ android:id=”@+id/Button01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button> 
<Button android:text=”button02″ android:id=”@+id/Button02″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button> 
</LinearLayout> 

这个main.xml里的LinearLayout也是有两个子元素Button01和Button02。Button01的android:layout_gravity设为”left”,Button02的 android:layout_gravity设为”right” 
java代码: 
复制代码代码如下:

<?xml version=”1.0″ encoding=”utf-8″?> 
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” 
android:orientation=”vertical” 
android:layout_width=”fill_parent” 
android:layout_height=”fill_parent” 

<Button 
android:layout_gravity=”left” 
android:text=”button01″ 
android:id=”@+id/Button01″ 
android:layout_width=”wrap_content” a 
ndroid:layout_height=”wrap_content”></Button> 
<Button 
android:layout_gravity=”right” 
android:text=”button02″ 
android:id=”@+id/Button02″ 
android:layout_width=”wrap_content” 
android:layout_height=”wrap_content”> 
</Button> 
</LinearLayout> 

FameLayout布局 
FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象—比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后 一个子元素是透明的)。

xml属性 
1,用xml文件来定义界面,然后Activity的setContentView方法根据xml文件里的定义来创建真正的控件对象。好比说xml文件是设计图纸,setContentView是生产机器,它依照图纸生产出各种各样的杯具和洗具。 
2,FrameLayout的xml属性来自三个地方:继承下来的,嵌套类定义的,自身类定义的。 
3,具的属性可查阅官方文档。下面是刚才遇到的几个属性。 
java代码: 
复制代码代码如下:

android:id 
//这个xml属性是继承自android.view类的xml属性。它为framelayout提供一个唯一识别符号,之后,程序要用到它时可以用View.findViewById() 或Activity.findViewById()来找到它。 
android:layout_width: 布局宽 
android:layout_height: 布局高 
//它们的取值是fill_parent或wrap_content。 
fill_parent :在x轴或则y轴上充满父容器的空间。 
wrap_content :framelayout里的元素有多少宽高就多少宽高, 
//这两个属性都是定义在android.widget.FrameLayout的嵌套类android.widget.FrameLayout.LayoutParams里。 
android:background:背景 
android:foreground :前景 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表