如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,以下是我总结的使用<include /> 标签来重用layout代码的方法:
首先:定义公共的layout:testlayout.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="wrap_content"
android:layout_gravity="left"
android:orientation="vertical"
>
<TextView android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaaaaaaaaaaa"
android:textSize="24sp"
/>
<TextView android:id="@+id/tv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bbbbbbbbbbb"
android:textSize="24sp"
/>
<TextView android:id="@+id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cccccccccccccc"
android:textSize="24sp"
/>
</LinearLayout>
再次,我们定义调用的xml:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<include
android:id="@+id/include_1"
layout="@layout/testlayout" />
<include
android:id="@+id/include_2"
layout="@layout/testlayout" />
<include
android:id="@+id/include_3"
layout="@layout/testlayout" />
<!-- <include layout="@layout/testlayout"/>
-->
</LinearLayout>
最后就是在代码中对其中组件的使用了:
如果我们只include“一次“
公用的testlayout.xml
,则直接获取其中的组件即可改变其值:
TextView test = (TextView) findViewById(R.id.tv_1);
test.setText("testtext");
如果我们只include“多次“
公用的testlayout.xml
(比如示例代码中),则不能
直接获取其中的组件改变其值:
例如我们要修改第三个include标签中textview的值,代码如下:
View view3 = findViewById(R.id.include_3);
TextView test = (TextView) view3.findViewById(R.id.tv_1);
test.setText("testtext");
这样我们就可以对其中各个include的layout组件的值进行修改。
分享到:
相关推荐
Android UI优化之include标签使用,主要介绍include使用方法及注意事项。
这个demo主要讲解了UI的一些优化方法,希望可以帮助需要的同学.
android 高级ui抽样布局中include标签的学习demo
为了高效复用完整布局,你可以使用<include>和标签嵌入另一个布局到当前布局。所以当你通过写一个自定义视图创建独立UI组件,你可以放到一个布局文件里,这样更容易复用。 复用布局因为其允许你创建可复用的复杂布局...
的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...
Android平台提供了大量的...简单的,你可以通过书写一个自定义的View来创建一个UI组件,但更简单的方式是仅使用XML来实现。 在Android XML布局文件里,一般,每个标签都对应一个真实的类实例(这些类一般都是View的子类
软件借鉴于video-live重新架构了软件,重新设计了UI,抽取了基类,使用了Mvp的开发模式,加入了Rxjava,使用IjkPlayer进行视频播放,替换了原项目中的vitamio。由于偷懒弹幕模块摘自于video-live,使用的是...
登入后使用Fragment碎片化,通过按钮进入不同页面,使用<include拼接两个xml文件形成登入后的主界面。拥有下拉菜单,聊天界面拥有一个聊天机器人,使用图灵官网提供的api,每次进入都会以不同的话欢迎使用者 此部分...
tinyui用于数以百万计的用户在上玩的几种游戏(网络,iOS,Android) 安装 haxelib install tinyui 使用 宏构建类src/UI18All.hx // all things in-scope is accessible in xml, include static extension ...
app开发过程中,会遇到不同页面里有相同的布局,这时我们可以将这些通用的布局提取出来到一个单独的layout文件里,再使用<include>标签引入到相应的页面布局文件里,主要通过include的layout属性引用。 举个栗子...
A UI widget that allows for headers and footers on lists backed by RecyclerView, for Android. Download Grab the artifact via JCenter. Include JCenter as a repository in your build.gradle file: ...
Recently updated to include Android Jelly Bean, Android Apps for Absolute Beginners, Second Edition takes you through the process of getting your first Android apps up and running using plain English...
Include a Windows Mobile application within your standard Xamarin.Forms application Extend your applications using the Xamarin.Forms Labs library In Detail Xamarin is an IDE used for the development ...
frameworks/base/include/ui/目录中Camera相关的头文件有以下几个: Camera.h CameraParameters.h ICamera.h ICameraClient.h ICameraService.h CameraHardwareInterface.h Frameworks/base/libs/ui目录中...
导入SystemUI到eclipse SystemUI路径:repo / frameworks / base / packages / SystemUI /一·通过Android.mk文件查找依赖库LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_...
It also introduces 2D graphics and UI design, as well as multimedia in Android mobile apps.The second part of the book delves into advanced mobile app optimization, including an overview of mobile ...
在Android开发中,很容易就能创建出复杂的UI结构,结果呢,用了很多的View,且其中的一些很少使用。针对这种情况,谢天谢地,Android还为我们提供了一个特别的构件——ViewStub,它可以使你充分享受<include>的好处...
HarmonyOS和android HarmonyUI A ui library for HarmonyOS, include animations, widgets and componentContainers. ValueAnimator 支持int和float数值设置,支持component的平移,缩放,旋转,透明度和布局尺寸的...
As of now, this is only a UI library. This library does not provide any social network functionality. That you have to code yourself. Screenshots How to use Include the library You can either ...