当前位置: 移动技术网 > IT编程>移动开发>Android > Android 控件(button)对齐方法实现详解

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

缘来有你,老封神榜全集,风起苍岚

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 :前景

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网