首页 技术 正文
技术 2022年11月7日
0 收藏 808 点赞 889 浏览 3354 个字

 android 常用布局

LinearLayout(线性布局)       
线性的 垂直的 水平的
RelativeLaytout(相对布局)    
最灵活的
TableLayout(表格布局)     
使用GridView代替
AbsoluteLayout(绝对布局)    
最好不要使用 绝对坐标
Framelayout(帧布局)      
布局叠加时使用 比如视频缓冲的环形滚动条
使用频次
Absolute<Table<Frame<Linear<Relative
android布局原则
(1)尽量多的使用线性布局和相对布局不要使用绝对布局
(2)在布局层次一样的情况下,建议使用线性布局代替相对布局因为线性布局性能稍高一点
(3)将可复用的组件抽取出来,并通过include标签使用
(4)使用viewstub标签加载一些不常用的布局 (暂时不使用的)
(5)使用merge标签减少标签的嵌套层次

<include/>的使用
作用:将公用的组件抽取出来单独一个xml文件,然后使用include标签导入公共布局
效果:提高ui的制作和复用效率
通过findViewById 也可以找到view 因为通过include实际上是将自布局直接包含进公共布局当中1,子布局title.xml
<?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="30dp"
android:background="#243821">
</LinearLayout>

  2,主布局main.xml

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

使用merge合并ui布局
作用:合并ui布局,使用该布局能降低ui布局的嵌套层次
场景1;布局根节点是FrameLayout且不需要设置backgroud和padding等属性,可以用merge代替
场景2:某布局作为自布局被其他布局include时,使用merge当做该布局的顶接点,这样再被引入时,
顶接点会自动被忽略
1,子布局progress.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progress"
android:layout_gravity="center"/>
</merge>

  2,主布局main.xml

<LinearLayout
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical">
<include layout="@layout/title"
></include> <FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <include layout="@layout/progress"/>
</FrameLayout>
</LinearLayout>

使用viewstub惰性加载
作用:viewstub标签同incude标签一样可以用来引入一个外部的布局,不同的是viewstub引入的布局默认不会扩张,
既不会占用显示也不会占用位置,从而在解析laytou时,节省cpu和内存
1,子布局common.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView
android:id="@+id/text"
android:text="viewstub"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

  2,主布局main.xml

<LinearLayout
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical">
<include layout="@layout/title"
></include> <FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <include layout="@layout/progress"/>
</FrameLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="按钮"
/>
<ViewStub
android:id="@+id/viewStub"
android:layout="@layout/common"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

  3,MainActivity

public class MainActivity extends Activity {    private Button btn ;
private ViewStub viewStub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
viewStub = (ViewStub)findViewById(R.id.viewStub);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewStub.inflate();
}
});
}
}
 
  
 
 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,040
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,526
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,373
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,154
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,787
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,869