《你的第一行Android代码》菜鸡的自学日记-第八天

本文深入讲解了Android开发中常用的四种布局:线性布局(LinearLayout)、相对布局(RelativeLayout)、帧布局(FrameLayout)及百分比布局。详细介绍了各布局的属性与使用场景,帮助开发者灵活掌握布局技巧。

四种基本的布局

1.线性布局:LinearLayout

通过三个Button来演示:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 3"/>
</LinearLayout>

android:orientation用于指定排列的方向,horizontal代表控件将在水平方向排列,vertical代表控件将垂直排列。

vertical 与 horizontal
手机运行效果 手机运行效果

android:layout_gravity则会指定控件在布局中的对齐方式,在LinearLayout布局中,当排列方向是水平方向时,只有垂直方向上的对齐方式才会生效,而水平方向的对齐方式却无法指定,反之亦然。

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      * android:layout_gravity="top"
        android:text="button 1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      * android:layout_gravity="center_vertical"
        android:text="button 2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      * android:layout_gravity="bottom"
        android:text="button 3"/>
</LinearLayout>

手机运行效果

(个人认为非常好的一个属性)android:layout_weight按比例指定控件大小。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    <EditText
        android:id="@+id/input_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Type Something"
        />
    <Button
        android:id="@+id/send"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="send"/>

</LinearLayout>

android:layout_weight后面的数字“1”用于决定其比例,系统会吧LinearLayout下面所有的layout_weight值相加,作为分母,各个控件自己layout_weight的值则作为分子,然后按比例来决定大小。(但是在实际操作时还是考虑一下排版在否美观,灵活运用)
手机运行效果

2.相对布局RelativeLayout

与LinearLayout相比较起来更加灵活。

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button 1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button 2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button 3"
        />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="Button 4"
        />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Button 5"
        />

</RelativeLayout>

手机运行效果

或者凑在一起热闹些:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button 3"
        />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/button3"
        android:layout_toLeftOf="@id/button3"
        android:text="Button 1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button3"
        android:layout_above="@id/button3"
        android:text="Button 2"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button3"
        android:layout_toLeftOf="@id/button3"
        android:text="Button 4"
        />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button3"
        android:layout_toRightOf="@id/button3"
        android:text="Button 5"
        />

</RelativeLayout>

手机运行效果

3.帧布局FrameLayout

死板的布局,所有的控件都默认放在布局的左上角。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
  <TextView
      android:id="@+id/text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="this is textview"
      />
    <ImageView
        android:id="@+id/img_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"		//自带的图片
        />

</FrameLayout>

手机运行效果

也可以像LinearLayout一样使用layout_gravity来指定控件的对齐方式:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
  <TextView
      android:id="@+id/text_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="left"
      android:text="this is textview"
      />
    <ImageView
        android:id="@+id/img_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@mipmap/ic_launcher"
        />

</FrameLayout>

手机运行效果

4.百分比布局

需要在app/build.gradle的dependencies闭包中增加内容:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    
  * compile 'com.android.support:percent:24.2.1'
    
    testCompile 'junit:junit:4.12'
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <Button
      android:id="@+id/button1"
      android:text="Button 1"
      android:layout_gravity="left|top"
      app:layout_widthPercent="50%"
      app:layout_heighPercent="50%"
      />
  <Button
      android:id="@+id/button2"
      android:text="Button 2"
      android:layout_gravity="right|top"
      app:layout_widthPercent="50%"
      app:layout_heighPercent="50%"
      />
  <Button
      android:id="@+id/button3"
      android:text="Button 3"
      android:layout_gravity="left|bottom"
      app:layout_widthPercent="50%"
      app:layout_heighPercent="50%"
      />
  <Button
      android:id="@+id/button4"
      android:text="Button 4"
      android:layout_gravity="right|bottom"
      app:layout_widthPercent="50%"
      app:layout_heighPercent="50%"
      />


</android.support.percent.PercentFrameLayout>

如果不是最新的Android2.2会报错,而Android2.2已经修复了这个问题。

。。。。。。

为什么我的Android Studio 2.2 就不行。。。(待解决)

小结:了解了四种基本布局,线性布局LinearLayout,相对布局RelativeLayout,帧布局FrameLayout以及出了问题的百分比布局,以及一些指定控件大小的属性。

本项目是一个基于安卓的日记本项目源码,本站之前介绍过很多关于日记本/备忘录/便签这一类型的项目源码。进入应用首先进入欢迎界面会有一个开门效果,点击”进入日记”即可进入主界面,在主界面可以点击进入”写日记”、”查看日记”、”搜索日记”、”日记加密”、”退出”以及右下角path单按钮查看更多。进入”写日记”界面即可写日记并且可以选择当天天气情况,写完日记以后不需要其他操作直接点返回键就可以自动保存内容并回到主界面。进入”查看日记”界面即可查看写过的日记,若没有写过日记,则提示用户写日记。进入”搜索日记”界面即可对日记内容进行搜索,搜索日记功能可以根据关键字模糊搜索并且可以即时出现结果。进入”日记加密”界面即可对日记进行加密,密码保护部分可以设置日志的数字密码或者图形密码,设置完成退出应用以后再次打开应用就会出现要求输入数字密码或者绘制图像密码的界面。点击右下方按钮会弹出弧形单,可进入相应操作。如关于、帮助、夜间模式、换背景、设置提醒、意见反馈。”换背景”操作,手动换屏,长按图片或者按单键按提示操作即可。”设置提醒”可以设置提醒写日记的时间。不得不说在本项目的开发过程中作者考虑的情况很周全,对用户体验方面也下了很大功夫。例如无需手动保存、可以选择天气、带有密码设置、即时搜索出结果等等功能都可以给使用者提供不错的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值