android include 共用布局
  HvTJUzsxOBtS 2023年11月25日 23 0



文章目录

  • 1、简介
  • 2、include xml


1、简介

为了优化Android 界面加载

2、include xml

假设我们现在有一个 textView多个界面公用,如果每个界面都写一个 那么 就比较耗费时间,耗费资源,我们通过 include 的方式 来进行 公用资源的加载

  1. 代码结构

2)until_text.xml 文件

公用的 textView

android  include  共用布局_xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello Unit Test"
    android:textSize="30dp"
    android:textColor="@color/colorAccent">

</TextView>

3)activity_main.xml 文件

android  include  共用布局_xml_02

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.lum.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="40dp"
        android:onClick="onClick"/>

    <include
        layout="@layout/until_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:textSize="90dp"/>
</LinearLayout>

我们看到 include 里设置的 android:textColor , android:textSize 并没有生生效,而且还是textView 内部的
android:layout_width ,android:layout_height 生效了 是因为 until textView 里面也有。
android:layout_* 如果也想生效 在 公共的 xml里也要有

4)activity_second.xml 文件

android  include  共用布局_android_03

<?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">

    <include
        layout="@layout/until_text"/>
</LinearLayout>



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月25日 0

暂无评论

推荐阅读
HvTJUzsxOBtS