Android: How to use tools with include layout

有些话、适合烂在心里 提交于 2019-12-09 09:02:11

问题


How do I use tools:

xmlns:tools="http://schemas.android.com/tools"

With <include>?

I have a layout A that I use tools to populate all the text fields with test data. And I have layout B that use include to copy layout A in to it. How ever when I do that I do not see the test data of A.

How can I see the test data of A included in B?

*Both layouts have xmlns:tools="http://schemas.android.com/tools, I even pushed it to layout tag.


回答1:


Check this link, Android tools attributes. It should give you an idea as to how to use the tools attributes. Specifically look at the tools:showIn attribute. It basically allows you to render layout_A in layout_B, in which layout_B has <include layout="@layout/layout_A"/> somewhere in the xml.

Here's an example:

layout_A

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/layout_B"
    >

<!-- Your layout code goes here -->

</RelativeLayout>

layout_B

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

<include layout="@layout/layout_A"/>

<!-- Your layout code goes here -->

</RelativeLayout>



回答2:


add layout_width and layout_height to the include tag , otherwise you will find it difficult to arrange it in an RelativeLayout



来源:https://stackoverflow.com/questions/23943184/android-how-to-use-tools-with-include-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!