首页 > 其他分享 >每日记录

每日记录

时间:2023-04-26 20:44:06浏览次数:29  
标签:layout 记录 每日 height width button android id

今天学习了android studio的基础

 

1.认识目录


以FirstActivity为例子,我们需要掌握的文件有:

manifest
java
drawable
layout
values
①manifest文件夹里面只有一个AndroidManifest.xml文件,在这个文件里,我们是对整个app进行一些设置,例如app的logo,app一进去的启动页面,app的名字...

 

②java文件夹里面是.java文件,负责整个app的逻辑处理,是完成整个app的核心所在。java文件真的超级powerful,后续会慢慢用例子体现,现在说一大堆显得有点空洞。

TIPS: 初学者一般再Java文件建立好了之后,不要随便移动它的位置。

③drawable文件夹里面放app需要用到的图片

④layout文件夹里面放的是“画页面”的.xml文件,里面的文件也叫布局文件。如果你会html&css&js的话,就很好理解了。.xml文件的作用就和.html和.css文件的作用类似,页面需要什么组件?怎么布局?用一些什么样式?都在.xml里面设置。但是对于一些复杂的样式,在.xml文件里面可能设置不了那么精美,就可以在java文件里面设置。

⑤value文件夹里面放了一些字符串,颜色等常量,例如:

//color.xml
<resources>
<color name="colorPrimary">#3F51B5</color>
</resources>


//firstActivity.xml
<Button
android:background="@color/colorPrimary"
/>
对于颜色#3F51B5,我给它起名字叫colorPrimary,之后我在布局文件中用这个颜色,就只需要喊它的名字colorPrimary就可以了。

最后总结一些基本思想:

layout和java文件夹是最重要的。如果把app比作一个人,layout就是一个人的脸,java就是一个人是灵魂。前者决定了这个app长什么样子,后者决定了这个app可以实现那么功能。
对于初学者,AndroidManifest.xml和value文件夹的作用不用着急掌握,到了某一天你需要实现某个功能需要用到这些,你就可以真真切切的知道它的用处了。
2.RelativeLayout
AndroidStudio里面支持的布局有挺多种的,但是最最重要的是RelativeLayout(相对布局)和LinearLayout(线性布局),熟练掌握这两种布局也非常够用了,当然还有GridLayout...但是对于初学者,先学会了相对布局和线性布局,再去学习其他布局,就会觉得非常简单轻松了。还有一个非常有用的布局,叫RecyclerLayout,因为要结合adapter使用,所以对于初学者略难,这里就先不讲了,之后会非常详细的介绍它。

学习布局需要掌握的东西很简单,就是它有的属性,以及取不同属性值可以达到的效果,下面我就慢慢列出来。

layout_width
layout_height
这两个属性就决定了布局的宽度和高度,把RelativeLayout想象成一个相框或者一个容器,在这个相框里面可以装其他的组件。对于嵌套在相框里面的组件,其所在的相框就是它的父空间。这个相框的大小呢,就用上面这两个属性举例,取值有三种:

wrap_content 刚刚把文字组件包裹满的长度
match_parent 撑满整个父空间的长度
100px 具体的像素值
对于相对布局有一个地方要注意!!!

相对布局里面的组件需要设置id(在同一个.xml文件里面的所有组件,其id不可以重复哦~)然后用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">

<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
/>

<Button
android:id="@+id/button_2"
android:layout_below="@id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
/>
</RelativeLayout>

例如上面这个例子,在RelativeLayout里面有两个按钮,第一个按钮的id是button_1,android:id="@+id/button_1",第二个按钮的id是button_2,为button_2设置了android:layout_below="@id/button_1"表示按钮2在按钮1的下面。所以效果图如下:

 

如果我不为按钮2设置相对向下的对象会怎样呢?也就是删掉android:layout_below="@id/button_1"。答案是按钮二会覆盖按钮一。

 

如果想让按钮居中怎么办?答案就是为RelativeLayout添加一个属性 android:gravity="center"

 

如果继续追问,我希望按钮一和按钮二并排在一起怎么办?答案是:sorry,RelativeLayout做不到啊~~

但是LinearLayout可以做到哦!

RelativeLayout还有很多其他的属性,可以自己试着玩,重要的属性就是上面这些,我用红色的粗体标记啦~

3.LinearLayout
线性布局要灵活一些,在实际应用上也是最最最广泛的。

layout_width
layout_height
和相对布局一样的用法和属性值,我就不赘述了!

区别于RelativeLayout,LinearLayout就不要求每个组件都要设置自己的id了,但是最好还是设置一下,这是一个好习惯哦。

那么问题来了,我怎么设置两个组件是横着并排还是竖着并排呢??现在就隆重介绍线性布局的重要属性 orientation

取值有两种:vertical(垂直)和 horizontal(水平)

<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:id="@+id/button_1"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 1"
/>

<Button
android:id="@+id/button_2"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 2"
/>

<Button
android:id="@+id/button_3"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 3"
/>

</LinearLayout>

 

android:orientation="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="vertical"
>

<Button
android:id="@+id/button_1"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 1"
/>

<Button
android:id="@+id/button_2"
android:layout_width="200px"
android:layout_height="100px"
android:text="Button 2"
/>

</LinearLayout>

 

android:orientation="vertical"决定了容器里的组件都是垂直排列,这就很好理解了。

线性布局还有一个重要的属性 layout_weight 取值一般是1、2、3...表示权重的大小,例如:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Button
android:id="@+id/button_1"
android:layout_width="200px"
android:layout_height="100px"
android:layout_weight="1"
android:text="Button 1"
/>

<Button
android:id="@+id/button_2"
android:layout_width="200px"
android:layout_height="100px"
android:layout_weight="2"
android:text="Button 2"
/>

</LinearLayout>

 

LinearLayout的强大就在于它是可以嵌套的,从而实现很多复杂的布局。

为了巩固你对它的认识,我出一个小小的题目,这是我的课程设计的一个页面,你会怎么设计这个布局呢??


————————————————
版权声明:本文为CSDN博主「萌萌怪」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42183184/article/details/82528910

标签:layout,记录,每日,height,width,button,android,id
From: https://www.cnblogs.com/xiao-hong111/p/17357222.html

相关文章

  • 每日打卡一小时(第十七天)
    一.问题描述pta多态实验:1.定义一个整数加法器类Adder,对其重载运算符“+”、“++”,main(void)函数完成对其的测试。#include<iostream>usingnamespacestd;/*请在这里填写答案*///主函数intmain(void){intx;Addera1,a2(a1);cin>>x;(a1++).show......
  • django token 认证 简单记录
    classUser(AbstractUser):username=models.CharField(max_length=20,unique=True,primary_key=True,verbose_name="用户名")email=models.EmailField(max_length=256,null=False,verbose_name="邮箱",blank=True)pass......
  • Profiler 记录
    GC.MarkDependencies:是由Resources.UnloadUnusedAssets引起的。该函数的主要作用是查找并卸载不再使用的资源。游戏场景越复杂、资源越多,该函数的开销越大,一般在300~2000ms范围内。Loading.UpdatePreloading:Unity引擎最主要的加载函数。该项一般在切换场景时或主动动态加载资源......
  • qiankun vue子应用升级webpack5问题记录
    升级的方式是使用最新版本的vue-cli脚手架,重新创建一个新项目,然后复制@vue/cli-xxx,vue相关依赖最新版本到子应用项目->核对babel,eslint相关配置的变化->重新安装依赖,处理出现的相各种异常情况->启动项目检查功能是否正常。本次升级主要是为了使用webpack5。以下......
  • vue-router3.x和vue-router4.x相互影响的问题记录
    背景项目中有一个系统使用的微前端,主站使用是vue2实现的,使用的是vue-router3.x。子应用有使用vue3实现的,使用的为vue-router4.x。该子应用中的页面A有通过操作按钮触发跳转到其他子应用页面B的需求,此时使用的是vue-router4.x的编程式导航API。当通过点击主站的Tab切换回B的时候......
  • 记录-Vue移动端日历设计与实现
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助工作中遇到一个需求是根据日历查看某一天/某一周/某一月的睡眠报告,但是找了好多日历组件都不是很符合需求,只好自己手写一个日历组件,顺便记录一下。先看看UI给的设计图和,需求是有数据的日期做标记,可以查看某一周/......
  • 每日打卡-14
    一.问题描述  请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数getArea()、计算对象周长的函数getPerim()。  在此基础上,通过继承Rectangle得到一个新的类Square,然后在Shape中增加一个函数intgetVertexCount()const用来获得当前......
  • 高分记录片
    美丽中国地球脉动蓝色星球像乌鸦一样思考微观世界此话怎讲迁徙的鸟河西走廊中国唱诗班典籍里的中国故宫100汉字五千年大国崛起小小少年书简阅中国人类星球猫咪物语......
  • rollup plugin功能记录
    plugin功能rollup-plugin-typescript2将ts转换为js@rollup/plugin-node-resolve解析导入导出并找到对应的文件@rollup/plugin-babel高级语法转换为低级语法(同时需要安装@babel/core,@babel/preset-env),配合@babel/preset-react处理react@rollup/plugin-co......
  • TF-GNN踩坑记录(四)
    目录引言题外话(MapFeatures使用)节点特征变换边特征变换传入额外参数问题问题demo解决方案引言由于图数据结构问题,直接使用Tensorflow的一些层是无法直接处理图数据的,需要借用TF-GNN框架下的MapFeatures对图数据中的节点特征或是边特征进行变换。题外话(MapFeatures使用)节点......