Android线性布局与相对布局的实现

目录
  • 线性布局(LinearLayout)
  • 相对布局(RelativeLayout)
  • 总结

线性布局(LinearLayout)

名字 含义
android:id 设置一个id方便使用
android:layout_width 宽度
android:layout_height 高度
android:background 设置背景颜色
android:layout_margin 设置外边距
android:layout_padding 设置内边距
android:orientation 设置方向(水平或者垂直)

练习代码

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

    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:background="#000000"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="50dp"
        android:paddingBottom="10dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0033"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal"
        android:background="#0066FF"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp">

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:layout_weight="1"/>
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#FF0033"
            android:layout_weight="1"/>
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#55AA99"
            android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>

实现效果

相对布局(RelativeLayout)

最常用属性

名字 含义
android:layout_toLeftOf 在什么的左边
android:layout_toRightOf 在什么的右边
android:layout_alignBottom 跟什么底部对齐
android:layout_alignParentBottom 与父控件底部对齐
android:layout_below 在什么的底部

样例效果

<?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">

    <View
        android:id="@+id/view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#000000" />

    <View
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@id/view_1"
        android:background="#FF0033" />

    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/view_2"
        android:background="#0066FF"
        android:orientation="horizontal"
        android:padding="15dp">

        <View
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#FF0033" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:padding="15dp">

            <View
                android:id="@+id/view_3"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#FF9900"/>
            <View
                android:id="@+id/view_4"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#FF9900"
                android:layout_toRightOf="@id/view_3"
                android:layout_marginLeft="10dp"/>

        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

实现效果

总结

到此这篇关于Android线性布局与相对布局的实现的文章就介绍到这了,更多相关Android布局内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Android手机开发 使用线性布局和相对布局实现Button垂直水平居中

    居中呢,这里分两种不同布局方式的居中!分别是 LinearLayout 和RelativeLayout. 一.首先说的是LinearLayout布局下的居中: 注意:android:layout_width="fill_parent" android:layout_height="fill_parent" 属性中,若水平居中,至少在宽度上占全屏:若垂直居中,则在高度上占全屏 <LinearLayout android:layout_width="fi

  • android Activity线性布局和表格布局实例讲解

    实验中只需要编写相应的xml的代码,java代码不需要更改,因为我们这里只是练习android的界面设计. 线性布局:线性布局就是将各种控件按照行或者列依次进行排列.其中本实验用到的各控件的属性解释如下:android:layout_weight属性是指不同的控件在activity中占有体积大小的比例.android:paddingLeft指内边距左的距离,即控件内文字离控件左边边界的距离.其它的类推.android:gravity指控件内文字相对于控件本身的方向属性,长度为dip,与像素独立的

  • Android线性布局与相对布局的实现

    目录 线性布局(LinearLayout) 相对布局(RelativeLayout) 总结 线性布局(LinearLayout) 名字 含义 android:id 设置一个id方便使用 android:layout_width 宽度 android:layout_height 高度 android:background 设置背景颜色 android:layout_margin 设置外边距 android:layout_padding 设置内边距 android:orientation 设置方向(

  • Android编程动态加载布局实例详解【附demo源码】

    本文实例讲述了Android编程动态加载布局的方法.分享给大家供大家参考,具体如下: 由于前段时间项目需要,需要在一个页面上加载根据不同的按钮加载不同的布局页面,当时想到用 tabhot .不过美工提供的界面图完全用不上tabhot ,所以想到了动态加载的方法来解决这一需求.在这里我整理了一下,写了一个 DEMO 希望大家以后少走点弯路. 首先,我们先把界面的框架图画出来,示意图如下: 中间白色部门是一个线性布局文件,我喜欢在画图的时候用不同的颜色将一块布局标示出来,方便查看.布局文件代码如下:

  • Android编程之代码创建布局实例分析

    本文实例讲述了Android编程之代码创建布局使用方法.分享给大家供大家参考,具体如下: 大概描述一下效果:最外层是一个 RelativeLayout 里面有自定义个LinearLayout,每个LinearLayout有两个TextView.that's it !!! private void initView() { // 获取xml的RelativeLayout layout = (RelativeLayout) findViewById(R.id.liner); for (int i =

  • Android ListView自动显示隐藏布局的实现方法

    借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示隐藏Toolbar 首先给listView增加一个HeaderView,避免第一个Item被Toolbar遮挡. View header=new View(this); header.setLayoutParams(new AbsListView.LayoutParams( AbsListView.

  • Android中实现自动生成布局View的初始化代码方法

    在android开发过程中,界面布局是及其重要的,但同时也是复杂.有的时候我们急于实际运行查看布局效果.但是android的编译速度我实在不想吐槽啥,尤其在布局越来越复杂,项目越来越大,资源文件越来越多的情况下. 尤其是是android的view的初始化,findViewbyId 完全是体力活,我们完全可以根据布局文件自动生成View的初始化代码. 首先声明:   1.这是及其容易做到的,实用性性一般,但是在复杂布局和首次写初始化View代码的时候及其好用. 2.只能生成有id标签的view的初

  • Android 组合控件实现布局的复用的方法

    看到很多项目会有实现自己的标题栏的做法,通常的界面是左边按钮或文字,加上中间的标题和右边的按钮或文字组成的.比较好的一种做法是使用include标签,复用同一个xml文件来实现布局的复用.但是这种方法是通过代码的方式来设置标题,左右按钮等其他的属性,会导致布局属性和Activity代码耦合性比较高. 因此,我们要通过自定义View,继承ViewGroup子类来实现这样的布局,降低布局文件和Activity代码耦合性. 首先,我们需要写出布局文件layout_custom_titlebar.xml

  • Android实现快递物流跟踪布局效果

    先看效果 首页Activity public class TraceActivity extends AppCompatActivity { private ListView lvTrace; private List<Trace> traceList = new ArrayList<>(10); private TraceListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState

  • Android编程实现圆角边框布局效果的方法

    本文实例讲述了Android编程实现圆角边框布局效果的方法.分享给大家供大家参考,具体如下: 这里用的是TableLayout布局的.先看效果图 下面看下布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" android:layout_width=&

  • Android中使用ViewStub实现布局优化

    在Android开发中,View是我们必须要接触的用来展示的技术.通常情况下随着View视图的越来越复杂,整体布局的性能也会随之下降.这里介绍一个在某些场景下提升布局性能的View,它就是ViewStub. ViewStub是什么 ViewStub是View的子类 它不可见,大小为0 用来延迟加载布局资源 注,关于Stub的解释 A stub is a small program routine that substitutes for a longer program, possibly to

随机推荐