零基础学安卓编程

安卓开发入门教程-常用布局_布局引用

2020-09-04  本文已影响0人  蓝不蓝编程

关注 安卓007 ,免费获取全套安卓开发学习资料

什么是布局引用

布局引用不是一种新的布局哦,目的是复用某些布局文件(如多个界面中出现了完全相同的一块内容),减少代码量,同时便于后期维护,在一处修改,其他引用的地方都跟着变了.

样例

  1. 创建公共布局:activity_common_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2" />
</LinearLayout>

效果图:


  1. 在activity_main.xml中引用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我有两个按钮" />
    <include layout="@layout/activity_common_layout" />
</LinearLayout>

效果图:


  1. 在activity_main2.xml中引用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我也有两个按钮" />
    <include layout="@layout/activity_common_layout" />
</LinearLayout>

效果图:


完整源代码

https://gitee.com/cxyzy1/ImportLayoutDemo


安卓开发入门教程系列汇总

开发语言学习

Kotlin语言基础

UI控件学习系列

UI控件_TextView
UI控件_EditText
UI控件_Button
UI控件_ImageView
UI控件_RadioButton
UI控件_CheckBox
UI控件_ProgressBar

关注头条号,第一时间获取最新文章:


上一篇 下一篇

猜你喜欢

热点阅读