`
stephen830
  • 浏览: 2964651 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android在layout xml中使用include

 
阅读更多

 

 

在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的。例如:

 

<include layout="@layout/otherlayout"></div> 

 

Android开发的官方网站的说明在这里。 
其中,有提到:

 

Similarly, you can override all the layout parameters. This means that any android:layout_* attribute can be used with the <include> tag.

意思是任何android:layout_*属性都可以应用在标签中。

 

如果使用如下代码:

 

<Relativelayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Textview
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/somestring"
        android:id="@+id/top" />

    <include layout="@layout/otherlayout"
        android:layout_below="@id/top" />
</Relativelayout > 

 

发现include的otherlayout,并没有在如我们预期的在id/top这个TextView下面,而是忽略了android:layout_below属性。经过Google发现,很多人遇到类似的问题。

 

有解决方法是在include的外面再包一层LinearLayout,如下:

 

<Linearlayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/top" >

    <include layout="@layout/otherlayout">
</Linearlayout > 

 

在Statckoverflow上找到了更好的解决方法: 解答道:必须同时重载layoutwidth和layoutheight熟悉,其他的layout_*属性才会起作用,否这都会被忽略掉。上面的例子应该写成这样:

 

<include layout="@layout/otherlayout">
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_below="@id/top" />

 

 

分享到:
评论

相关推荐

    android xml中include标签的使用

    关于xml布局中标签&lt;include /&gt;的使用的一个Demo

    Android在layout xml中使用ViewStub完成动态加载问题

    一、Layout XML文件常见的两种模块加载方式  1、静态加载:被加载的模块和其它模块加载的时间一样。   &lt;include layout=@layout/otherLayout/&gt;  2、动态加载:需要被加载的模块初始时并没有被加载进内存,在你...

    Android开发之merge结合include优化布局

    布局文件都要有根节点,但android中的布局嵌套过多会造成性能问题,于是在使用include嵌套的时候我们可以使用merge作为根节点,这样可以减少布局嵌套,提高显示速率。 &lt;?xml version=1.0 encoding=utf-8?&gt; ...

    解析android中include标签的使用

    如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过&lt;include&gt; 标签来重用layout代码。app_title.xml: 代码如下:&lt;?xml version...

    Android代码-AndroidGradientUI

    2. Using in layout.xml GradientIconView First the custom attribute should declare in xml like this: xmlns:app="http://schemas.android.com/apk/res-auto" Then you can add GradientIconView in layout.xml ...

    新版Android开发教程.rar

    的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...

    Android代码-MoneyTextView

    Include MoneyTextView in a layout xml file: Include the following code in your Activity in order to update the amount value: moneyTextView.setAmount(156); More settings example Attributes ...

    自己平时做的android相关的总结

    声明:这些内容是逐步总结过来的,所以可能有当时的理解不正确,只希望...XML中的include标签加入后崩溃 Button的background标签使图像拉伸的问题 SharedPreferences 的getString 的陷阱 TextView 中的EMS和Maxlength

    高仿微信界面

    现在三个Fragment已经添加到了MainActivity中,滑动ViewPager切换Fragment,同时底部的导航栏也会切换,在为ViewPager添加监听以前,先说说底部导航栏。 第二步,底部导航。 这个的切换其实就是切换准备好的png...

    android使用include调用内部组件的方法

    本文实例讲述了android使用include调用内部组件的方法。分享给大家供大家参考。具体如下: 例子一: sublayout.xml &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;LinearLayout xmlns:android=...

    Android代码-相册特效

    SlidingCard ...1.Add the com.mxn.soul.slidingcard_core.ContainerView to your layout XML file. activity_main.xml: card_item_height need to be bigger than the height of your photo o

    Android代码-pin-edittext

    Include PinCodeEditText in your layout XML android:maxLength: The total pin (Required to make PinEditText limit its length) android:inputType: Preferred "textNoSuggestions" (If you want text) or ...

    Android布局技巧之include、merge与ViewStub标签的巧用

    app开发过程中,会遇到不同页面里有相同的布局,这时我们可以将这些通用的布局提取出来到一个单独的layout文件里,再使用&lt;include&gt;标签引入到相应的页面布局文件里,主要通过include的layout属性引用。  举个栗子...

    Android代码-LoadDataLayout

    在需要用到的布局中引入相关的Layout: 普通样式:LoadDataLayout 仿知乎样式:SwipeLoadDataLayout 支持的自定义属性: &gt; loadingText:加载页面显示的文本,可选,仅LoadDataLayout可用 &gt; &gt;

    LCRapidDevelop-master

    mRecyclerView内部集成的自动加载 上啦加载用不上 在其他View使用 progress = (ProgressActivity) findViewById(R.id.progress); //设置RecyclerView的显示模式 当前List模式 mRecyclerView.setLayoutManager(new...

    Android代码-Android日期小部件

    Include CalendarPickerView in your layout XML. This is a fairly large control so it is wise to give it ample space in your layout. On small devices it is recommended to use a dialog, full-screen ...

    Apress.Android.Apps.for.Absolute.Beginners.2nd.edition

    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...

    Android代码-SwipeStack

    Use the link.fls.swipestack.SwipeStack view in your XML layout file Set the parent view's clipChildren attribute to false Example: Create an adapter Create an adapter which holds the data and ...

    《Android应用开发》个人总结报告.doc

    线线布 局模式可以在比较简单的include进行完成,最重要的一点就是:我们可以自己通过重写 方法或者通过实现View或者Layout等类进行扩充项目需 要的布局(或者控件) ,在学习界面中,我发现Android为我们提供了很好的...

    Android代码-Android两级磁贴列表,拥有完美的不同级磁贴互相接触推动的效果,适合多级分类嵌套展示。

    Overview An Android library for double level section headers that stick to ...2. Replace your ListView with com.kenber.view.DoubleStickyHeaderListView in the layout.xml file. 3. Make the adapter for you

Global site tag (gtag.js) - Google Analytics