2

I tried to stack two layouts at each other but I can't seem to do it. I have one Java class file as a view, in this one there are balls which I can move by dragging at the screen. The other one is a XML file which is made by the Graphical Editor in Eclipse.

This is my code I used to display them at once:

LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.main, null);
    LinearLayout layout = new LinearLayout(this);
    // Define the LinearLayout's characteristics
    layout.setGravity(Gravity.CENTER);
    layout.setOrientation(LinearLayout.VERTICAL);
    // Set generic layout parameters
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    DrawView custom = new DrawView(this);
    layout.addView(view);
    layout.addView(custom, params); 
    setContentView(layout);

However, this does show the views both. My only problem is that they are not on top of each other. Now they are under each other. Also when they are stacked is there a way to make them both transparent (to see both layers)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Marc
  • 1,094
  • 3
  • 17
  • 38

4 Answers4

1

You're specifying in code that you want a LinearLayout. but to get its children to overlap you should use a RelativeLayout (or the decrecated absolute layout, and even a FrameLayout could work).

the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
mah
  • 39,056
  • 9
  • 76
  • 93
1

Use a FrameLayout as the parent layout and then add your views to the parent FrameLayout. In Frame all views are stacked. For the transparency you can set alpha to the background or a transparent background color to the views.

bostan
  • 373
  • 2
  • 8
0

You should use a RelativeLayout in stead of a LinearLayout as a parent. This will allow you to stack the Views on top of each other. The View that was added last to the parent will be on top. To set alpha of your views you can have a look at this question

Community
  • 1
  • 1
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
0

You need to set a background color on the top layer, and use the bring to front attribute to your top layer. The background color can be set '#BBCCCCCC' BB is from 00 to FF defines the transparency of the color. CCCCCC defines the color.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148