2012-03-23 174 views
0

我想堆叠图像视图旁边(70%重叠)。我使用了一个frameLayout,并给了每个elemnet填充10个。它的工作原理,但是这个填充在处理事件时会杀死我。有没有更好的方式重叠视图(使用不同的布局..等)?我开发的Android 2.3堆叠重叠的图像视图

<RelativeLayout android:layout_height="fill_parent" 
android:layout_width="fill_parent" > 

    <FrameLayout  
    android:id="@+id/frameLayoutBottom"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_alignParentBottom="true"  
    android:layout_centerHorizontal="true" > 
    <ImageView   
    android:id="@+id/ivBottom1"   
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:src="@drawable/c10" />  
    <ImageView  
    android:id="@+id/ivBottom2"   
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"       
    android:paddingLeft="10dp"   
    android:src="@drawable/c11" /> 

顺便说一句,当我尝试了保证金,而不是填充,图像依然对的海誓山盟100%顶级

+0

通过处理事件意味着什么?点击事件? – 2012-03-23 14:12:56

+0

是或更具体的触摸事件 – Snake 2012-03-23 14:53:16

回答

1

创建一个自定义的ViewGroup,在onLayout中展示它的子项。如果您搜索“自定义ViewGroup onLayout”,有相当多的例子。在一般情况下,onLayout方法重写如下:

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) {} 

内部的方法,你会希望使用getChildCount()和getChildAt(index)来获取你的孩子的看法引用。一旦你有了它们,你可以使用l,t,r和b值来比较你ViewGroup的边界,并找出布局子项的位置。

一旦找到孩子并计算出他们想要的位置,就可以使用child.layout(l,t,r,b)将子视图放入新的自定义ViewGroup中。

希望这会有所帮助。如果你真的需要破解成XML这一点,你可以尝试以下方法:

<RelativeLayout  
android:layout_width="fill_parent"  
android:layout_height="fill_parent"> 
<ImageView 
android:id="@+id/spacer1"   
android:layout_width="10dp"   
android:layout_height="wrap_content" />   
<ImageView  
android:layout_width="wrap_content" 
android:layout_height="wrap_content"   
android:src="@drawable/whatever1" /> 
<ImageView  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_toRightOf="id/spacer1"   
android:src="@drawable/whatever2" /> 
</RelativeLayout> 
+0

哦好的。这是有道理的,但是我试图在XML布局中做,而不是在代码 – Snake 2012-03-23 14:41:28

+0

编辑答案,包括一种方式来完成与XML。基本上,您可以使用RelativeLayout并在1px gif的时代声明一个“spacer”ala。 – drasticp 2012-03-23 16:10:20

+0

完美。感谢你的回答 – Snake 2012-03-24 04:22:45

2

我有这样的布局:

Hand of cards

我通过重载FrameLayout来创建手容器和ImageView来创建卡片。然后我可以决定他们如何以我想要的方式进行布局。

+0

这就是我想要显示的,但我需要一个指向某些代码的指针来查看你的意思 – Snake 2012-03-23 14:40:50

+0

我没有这台计算机上的代码。我回家后会发布它。 – CaseyB 2012-03-23 14:43:54

+1

请做(我会让它安息回答:) – Snake 2012-03-23 14:57:48