您的位置:首页 > 移动开发 > Android开发

Android布局之LinerLayout点滴

2015-12-15 12:13 633 查看
LinerLayout顾明思议,线性布局,指定VIEW只能按横向或者竖线进行依次排列。

android:orientation:vertical(竖向排列) ;horizontal(横向排列)

示例演示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3" />

</LinearLayout>


效果如下:



修改

android:orientation="vertical",再看下效果
</pre><img src="http://img.blog.csdn.net/20151215120612954?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><p></p><p></p><p>有了这个属性,开始我们的UI旅程吧,写一个计算器界面,参考系统计算器</p><p></p><p></p><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dip"
android:orientation="horizontal">

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_margin="2dip"
android:text="BackSpace" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:layout_gravity="center_vertical"
android:text="CE" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:layout_gravity="bottom"
android:text="C" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="7" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="8" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="9" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="/" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="4" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="5" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="6" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="*" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="1" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="2" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="3" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="-" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="0" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="+/-" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="." />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dip"
android:text="+" />

</LinearLayout>

</LinearLayout>


效果:


已经实现了计算器部分界面,自己补充完整即可。

不对,第一行排列为什么不整齐呢,小伙伴们也发现了

android:layout_weight="1"

android:layout_margin="2dip"

android:layout_gravity="bottom"

layout_weight属性:将各个VIEW宽度进行平均按比例分配;

layout_margin属性:与其它VIEW之间间距设置

lyaout_gravity属性:设置在父VIEW中的对其方式,重点说明,如果LinerLayout的orientation设置的为horizontal,则属性值top bottom 有效;如果设置为vertical则left right等有效。

让我们依次看下效果吧

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="1" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />

</LinearLayout>



button1占用50%控件,button2,3分别占用25%空间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: