Android属性和布局

Android属性和布局

项目文件结构

在这里插入图片描述

manifest文件是Android系统配置文件夹,包含AndroidManifest.xml文件

java文件夹:用来存放java代码,MainActivity是默认生成,另外两个是测试类

res文件夹存放Android资源文件,包含drawable图片资源文件,layout布局资源文件,mipmap图片资源文件夹,values存放数值资源文件

布局属性

在这里插入图片描述

xmlns属性全称为xmlnamespace,添加了这个属性才可以使用Android属性

layout_width/height即宽/高(父布局的宽高),布局属性必备

​ 属性值match_parent表示占据整个界面的宽

TextView是文本控件

​ wrap_content:即包裹文本内容

​ text属性表示文本的值

​ textSize:文本大小

在这里插入图片描述

Application标签:表示整个Android应用,在项目中是唯一的

​ allowBackup属性设置为true表示允许备份应用的数据;

​ icon属性设置l这个app在桌面上的icon图标;

​ lable属性设置了App在桌面上的显示名称

​ supportsRtl属性设置为true表示支持从右向左布局

​ theme属性为主题

activity 标签:一个Activity可以理解成一个app界面

​ name属性值为 .MainActivity,这里的 . 表示当前Activity的包名

​ intent-filter标签,此标签可以添加过滤

​ action标签:添加这个标签并设置其name属性为android.intent.action.MAIN,表示这个Activity为项目主
Activity,启动时作为主页面,首页启动。

​ category标签:设置其属性值为android.intent,category.LAUNCHER,项目将在手机程序列表中显示

Android布局属性值

​ padding意为填充,控件内部填充布局

​ margin意为边缘,控件外部距父控件的距离

​ gravity=“botton|right” 右下角

LinearLayout线性布局

​ orientation=“vertical” 表示该布局中所有子控件都应该垂直排列

​ orientation=“horizontal”表示水平布局

线性布局属性

gravity属性
layout_gravity

​ 控制控件在父布局中的位置(类似margin)

center_horizontal水平居中
right居右
left居左
gravity

​ 控制控件中内容的显示(和padding类似)

在这里插入图片描述

layout_weight属性
layout_width=“match_parent”

​ 变化layout_weight大小,当第一个TextView的layout_weight属性变大时,第二个的weight会变小,,内容被压缩

layout_width=“wrap_content”

​ 变化layout_weight大小,当第一个TextView的layout_weight属性变大时,第二个的weight会变大,内容不被压缩

layout_width=“0dp”

​ 系统推荐值,系统将采用另一种算法来计算控件的控件占比,这样layout_weight属性值和占据的“宽度”将成正比例

weightSum

​ 表示所有layout_weight的和,此属性在父布局种使用

​ 配合layout_width = "0dp"使用

​ ···

<?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"
    android:gravity="center"
    
    android:weightSum="6">

    <TextView
        android:layout_width="0dp"
        
        android:layout_height="wrap_content"
        android:layout_weight="2"
        
        android:text="2"
        android:background="#ff0000"
        android:textSize="30sp" />

    <TextView
        android:layout_width="0dp"
        
        android:layout_height="wrap_content"
        android:layout_weight="1"
        
        android:text="1"
        android:background="#E7D1D1"
        android:textSize="30sp" />

</LinearLayout>

···

效果图:

在这里插入图片描述

上述代码设置父布局weightSum为6,将整个屏幕分成6份,将第一个TextView的layout_weight属性值设置为2,第二个设置为1,分别占据2份和1份宽的屏幕,并且设置控件为垂直居中显示,结果可以看出第一个textView是第二个的两倍宽,且两个之和占总宽度的一半。

Android布局之相对布局——RelativeLayout

RelativeLayout常用位置属性

第一类:属性值为truefalse

android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离

<?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/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Butten1"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_above="@+id/btn1"
        android:layout_toLeftOf="@+id/btn1"
        
        android:text="Butten2"/>

    <Button
    android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
        
    android:layout_above="@+id/btn1"
    android:layout_toRightOf="@+id/btn1"
        
    android:text="Butten3"/>

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_above="@+id/btn2"
        android:layout_toLeftOf="@+id/btn3"
        android:layout_toRightOf="@+id/btn2"
        
        android:text="Butten4"/>


</RelativeLayout>

效果

在这里插入图片描述

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_alignParentBottom="true"
        
        android:padding="5dp"
        android:text="TextView1"
        android:textSize="26dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_alignParentLeft="true"
        
        android:padding="5dp"
        android:text="TextView2"
        android:textSize="26dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        
        android:padding="5dp"
        android:text="TextView3"
        android:textSize="26dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_alignParentRight="true"
        
        android:padding="5dp"
        android:text="TextView4"
        android:textSize="26dp"/>
</RelativeLayout>

效果:

在这里插入图片描述

Android布局之帧布局——FrameLayout

​ 一种比较简单的布局方式,所有的控件层叠显示,默认放在屏幕的左上交,最小添加的控件放在最底层,后添加的控件放在先添加的控件的上面。

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

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:background="#ff6143"/>

    <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:background="#7bfe00"/>

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="#ffff00"/>



</FrameLayout>

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值