6

With all the Android development tools greatness, especially as of version 21, the graphical layout editor is a powerful tool to have visual control over a layout with fragments for every configuration and locale. I'm aware that a typical Activity's layout XML will contain static fragment tags with info embedded for the layout editor, for example tools:layout="@layout/book_collection_view_window_list".

However, because I need to replace the Fragments in my window dynamically, I cannot use the static fragment tags, and instead need to provide containers, such as FrameLayout, to which I can refer in replace(). Is there something like tools:layout or tools:context I can use on this container so in the layout editor there is more than just black emptyness, and so I can leverage the design and verification features it would give me?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
pjv
  • 10,658
  • 6
  • 43
  • 60

1 Answers1

2

It looks, according to the documentation for FrameLayout, like there is an attribute you can use on the FrameLayout tag to put a drawable in the foreground of the ViewGroup.

You could use this to see the FrameLayout whilst you are setting up your view. I often just put a colour in so I can see where it is relative to the background and other Views.

SDJMcHattie
  • 1,690
  • 1
  • 15
  • 21
  • Not quite what I wanted, but it's a start. Since you seem to be using it already, could you include a line of code? Any thoughts on how to only show it in the tool and not when running (maybe make a custom view and do it programmatically?)? – pjv Feb 08 '14 at 13:28
  • I would use XML such as `android:background="#FF0000"` for red. If your `Fragment`s are opaque, you don't need to do anything to hide the red colour, but I would remove it from your XML once you've laid everything out. Alternatively, you can set the background of the `FrameLayout` at runtime when you add the `Fragment` using the `FragmentManager` by using code such as: `findViewById(R.id.frame_layout).setBackgroundColor(Color.TRANSPARENT);` – SDJMcHattie Feb 10 '14 at 12:47
  • Another solution is to sub class the `FrameLayout` and do something like override the `addChild()` methods and clear the background colour before calling `super()`. Or, if it's possible to identify in the constructor that the layout was instantiated by the tool, then set a random colour to the background programatically. It all seems overkill when you can just remove the colour after setting up your XML though. – SDJMcHattie Feb 10 '14 at 12:51