本篇文章给大家谈谈AndroidStudio分类整理res/layout中的布局文件,以及androidstudio布局类型的知识点,同时本文还将给你拓展2.3、AndroidStudio使用Layo
本篇文章给大家谈谈Android Studio 分类整理 res/layout 中的布局文件,以及android studio布局类型的知识点,同时本文还将给你拓展2.3、Android Studio使用Layout Editor设计UI、Androi Studio 之 LinearLayout、Androi Studio 之 RelativeLayout、Android Layout 布局文件里的 android:layout_height 等属性为什么...等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- Android Studio 分类整理 res/layout 中的布局文件(android studio布局类型)
- 2.3、Android Studio使用Layout Editor设计UI
- Androi Studio 之 LinearLayout
- Androi Studio 之 RelativeLayout
- Android Layout 布局文件里的 android:layout_height 等属性为什么...
Android Studio 分类整理 res/layout 中的布局文件(android studio布局类型)
•准备工作
新建一个名为 TestLayouts 的项目;
进入 Project 模式:
来到 TestLayouts/app/src/main/res/layout 文件夹下;
•分类整理 layout 中的文件
- 第一步:右击 layout->New->Directory:
- 第二步:右击 Main->New->Directory:
此时,你可能会发现,Android Studio 并没有乖乖的在 Main 文件夹下创建 layout 文件夹:
没错,Android Studio 自动帮我们合并了空文件夹,如何取消呢?
点击这个类似于设置的小齿轮,将 Compact Middle Packages 就好了;
此时,你在打开 layout 目录:
一切正常;
- 第三步:配置 build.gradle
需要注意的是,这个 build.gradle 必须是 Project 模式下的 app 里的 build.gradle;
在 android{} 中添加如下配置:
代码如下:
sourceSets { main { res.srcDirs = [ 'src/main/res/layout/Main', 'src/main/res/layout', 'src/main/res' ] } }
然后 Sync 一下就好了;
•layout文件夹报红?
按照上面的方式,将 layout 中的文件按照类别整理;
但是,layout 报红了:
问题暂未解决,有强迫症的我先暂时不分类整理了;
2.3、Android Studio使用Layout Editor设计UI
Android Studio提供了一个高级的布局编辑器,允许你拖拽控件,在编辑XML之后可以实时预览。
在布局编辑器中,你在文字视图和设计视图直接来回切换。
在文字视图中编辑
你可以在文字视图中编辑你的布局文件,这一节描述文字视图可用的一些特性。
预览
当你在文字视图中编辑室,你可以通过点击窗口右边的Preview面板来在设备中预览布局。在Preview面板,你可以在面板顶部更改多个选项来更改预览,包括预览设备,布局主题,平台版本等等。如图:
要想在多个设备中模拟预览布局,在设备下拉框选择Preview All Screen Sizes。
当你点击预览图片,布局编辑器自动高亮并且定位到相应的位置。
交互错误检测和恢复
当你编辑文字试图中的XML布局文件时,Android Studio会实时提示错误并提供帮助。
比如,假设你添加了一个按钮,却拼成Buttonn。Android Studio会通过显示一个如下的错误来帮你纠正它。
Android Studio可以提示你补充遗漏的信息。比如,假设你添加一个Fragment到你的XML文件中。首先,Android Studio显示了一个自动完成的提示。一旦确定你添加一个fragment,Android Studio会显示一个带链接的错误来提示你补充遗漏的属性。点击Automatically add all missing attributes可以帮助你添加丢失的属性。如图:
选择一个主题
通过点击主题图标来为你的app选择一个主题。
这会打开Select Theme对话框,在这里你可以选择合适的主题并在右边选择合适的。如图:
本地化
Android Studio提供了内建的本地化支持。当你点击本地化按钮,选择一个特定的语言,可以实时预览。如图:
在设计视图中编辑
你可以通过点击窗口地步的Design来切换到图形编辑器视图。如图:
多API版本预览渲染
Android Studio支持多API版本渲染,当你在设计视图中点击Android版本,Android Studio允许你同时多个API预览你的Android布局。如图:
截图
当你在一个连接的设备上运行你的app的时候,你可以通过点击设计视图中的相机图标完成对app界面的截图。
本文作者:宋志辉
个人微博:点击进入
Androi Studio 之 LinearLayout
LinearLayout
•常用属性
•注意事项
当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用
- android:layout_gravity="left"
- android:layout_gravity="right"
- android:layout_gravity="center_horizontal"
当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用
- 即: top , bottom , center_vertical 是生效的
Weight(转载)
•概念
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.说明:
- 指示LinearLayout中多少额外空间分配给与这些LayoutParams关联的视图
- 如果视图不应被拉伸,请指定0
- 否则,额外空间将在权重大于0的所有视图中按比例分配。
剩余布局大小(额外空间) = 父布局大小 - 子布局大小之和;
•详解
1.额外空间,指的是剩余空闲空间, 额外空间将在权重大于0的所有视图中按比例分配。
如下,总权重为1+1=2;
第一个控件是比第二个控件占的空间小的,即w(12345)+1/2空闲空间< w(123456)+1/2控件;
<LinearLayout android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_height" android:layout_weight="1" android:text="12345"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_height" android:layout_weight="1" android:text="123456"/> </LinearLayout>如果我们让控件的宽度定义为layout_width="0dp" ,这样比如2个控件的 layout_weight="1" 就可以各自50%平分整个空间了;
因为:0 + 1/2空闲空间 = 0 + 1/2空闲空间。
2.默认layout_weight为0,所以如果这么写:
<LinearLayout android:orientation="horizontal"> <TextView android:layout_width="40dp" android:layout_height="match_parent" android:background="#000" /> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:layout_width="40dp" android:layout_height="match_parent" android:background="#888" /> </LinearLayout>则总权重为1,即Button占了所有剩余空闲空间,无论它在哪个位置。
3.在排列方向上设置了match_parent, 如下,权重为2,1,2
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1" android:layout_weight="2"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2" android:layout_weight="1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3" android:layout_weight="2"/>运行结果如下:
分析:
- 因为设置的都是match_parent,所以如果没有设置权重,三个Button只会显示第一个,其他会被覆盖
- 但是设置了权重后, 我们就按三个 Button 给定的 width=match_parent 计算剩余空间
- 剩余空间=1个match_parent空间-3个match_parent空间= -2个match_parent空间(负2)
- 所以
- Button1所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/5 = 1/5个match_parent空间
- Button2所占空间 = 1个match_parent空间+(-2个match_parent空间)*1/5 = 3/5个match_parent空间
- Button3所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/5 = 1/5个match_parent空间
所以在统一设置match_parent时,会有这么一个特性,权重越大,空间越小。
而且在某个控件权重刚好为另外的所有控件权重之和时,这个控件会消失。
如权重变为1,2,3;
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1" android:layout_weight="2"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2" android:layout_weight="1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3" android:layout_weight="2"/>运行结果如下:
同样的算法:
- Button1所占空间 = 1个match_parent空间+(-2个match_parent空间)*1/6 = 2/3个match_parent空间
- Button2所占空间 = 1个match_parent空间+(-2个match_parent空间)*2/6 = 1/3个match_parent空间
- Button3所占空间 = 1个match_parent空间+(-2个match_parent空间)*3/6 = 0个match_parent空间
本次内容转载自:Android-0.Android Studio布局中layout_weight用法
divider(转载)
•为LinearLayout设置分割线
很多界面开发中都会设置一些下划线,或者分割线,从而使得界面更加整洁美观,比如下面的酷狗 音乐的注册页面:
对于这种线,我们通常的做法有两种:
- 直接在布局中添加一个view,这个view的作用仅仅是显示出一条线,代码也很简单:
<View android:layout_width="match_parent" android:layout_height="1px" android:background="#000000" />- 这个是水平方向上的黑线,当然你也可以改成其他颜色,或者使用图片
- 使用LinearLayout的一个divider属性
- 1)divider : 设置作为分割线的图片
2)showDividers : 设置分割线的位置
- none(无)
- beginning(开始)
- end(结束)
- middle(每两个组件间)
- 3)dividerPadding : 设置分割线的 Padding
本次内容转载自:LinearLayout(线性布局)
Androi Studio 之 RelativeLayout
RelativeLayout简介
•基本属性
•根据父容器定位
•父容器定位属性示意图
•根据兄弟组件定位
•根据兄弟组件定位
•margin(偏移)
•padding(填充)
•margin与padding的区别
首先margin代表的是偏移,比如 marginleft = "5dp" 表示组件离容器左边缘偏移 5dp;
而 padding 代表的则是填充,而填充的对象针对的是组件中的元素;
比如为 TextView 设置 paddingleft = "5dp",则是在组件里的元素的左边填充 5dp 的空间;
margin 针对的是容器中的组件,而 padding 针对的是组件中的元素,要区分开来。
Android Layout 布局文件里的 android:layout_height 等属性为什么...
有的时候,我们配置好的布局文件,在加载完成添加到我们的 Activity 中后发现,并没有安装我们设置的属性
来布局,比为我们设置了 android:layout_marginTop="100dip",但是运行程序后发现一点作用都没有,相似的还有 layout_height 等以 android:layout_开头的属性设置都没有作用,这类问题以我们使用 Adapter 的作为数据源的时候作用居多,因为 Adapter 里有一个方法是 getView, 这个返回的 VIew 是一个从 XML 布局里加载的,一般如下:
if(convertView==null){
convertView=LayoutInflater.from(mContext).inflate(R.layout.main, null);
}
return convertView;
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new FrameLayout.LayoutParams(getContext(), attrs);
}
关于Android Studio 分类整理 res/layout 中的布局文件和android studio布局类型的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于2.3、Android Studio使用Layout Editor设计UI、Androi Studio 之 LinearLayout、Androi Studio 之 RelativeLayout、Android Layout 布局文件里的 android:layout_height 等属性为什么...的相关信息,请在本站寻找。
本文标签: