Android使用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上找到了更好的解决方法:
解答道:必须同时重载layout_width和layoutheight熟悉,其他的layout*属性才会起作用,否这都会被忽略掉。上面的例子应该写成这样:

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

另外,关于xml的复用,还可以使用merge标签,merge标签主要是用来优化显示的,减少View树的层级,可以参考这里:https://developer.android.com/resources/articles/layout-tricks-merge.html 翻译版在这里:http://apps.hi.baidu.com/share/detail/20871363

原文链接:http://www.race604.com/using-include-in-layout/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Studio中,使用include来引入第三方库的方法如下: 1. 在项目根目录下的settings.gradle文件中,添加第三方库目录。例如,如果要引入名为PullToRefresh的库,可以在':app'后添加【, ':PullToRefresh'】。示例:include ':app', ':PullToRefresh'。\[1\] 2. 在app/build.gradle文件中的dependencies{}中添加编译依赖。可以使用compile project(':PullToRefresh')来引入项目中的库。示例:dependencies { compile project(':PullToRefresh') }。\[1\] 3. 确保被引入的项目包含一个build.gradle文件,并在其中指定相关配置。例如,指定compileSdkVersion和buildToolsVersion与当前项目一致。示例:apply plugin: 'android-library' android { compileSdkVersion 23 buildToolsVersion "23.0.0" ... }。\[1\] 另外,还可以使用implementation来引入aar依赖。在dependencies{}中添加implementation (name:'testlibraryfilename', ext: 'aar')来引入aar文件。示例:dependencies { implementation (name:'testlibraryfilename', ext: 'aar') ... }。\[2\] 如果aar文件位于libs目录下,可以在Android {}中指定aar文件所在的libs目录。在android{}中添加repositories{},并在其中使用flatDir指定libs目录的路径。示例:android { repositories { flatDir { dirs '../app/libs' } } }。\[3\] 总结起来,使用include来引入第三方库的步骤包括在settings.gradle中添加库目录,然后在app/build.gradle中的dependencies{}中添加编译依赖。同时,确保被引入的项目包含一个build.gradle文件,并在其中指定相关配置。另外,还可以使用implementation来引入aar依赖。如果aar文件位于libs目录下,可以在Android {}中指定aar文件所在的libs目录。 #### 引用[.reference_title] - *1* [关于Android Studio引入外部项目](https://blog.csdn.net/zoujiawei6/article/details/51354795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Android studio 使用aar](https://blog.csdn.net/hnjcxy/article/details/125333039)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值