2011-07-09 42 views
1

我已经在我的资源定义的自定义形状/绘制的文件夹:如何在视图内定位自定义形状?

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line"> 
    <stroke 
     android:color="#FF404040" 
     android:width="1dp" 
     android:dashGap="3dp" 
     android:dashWidth="3dp" 
    /> 
    <size 
     android:height="1dp" 
    /> 
</shape> 

我用它作为我的观点一个背景。该形状垂直居中定位在视图内,但我希望它出现在视图的底部,而不是有办法做到这一点?

+0

你可以发表你的布局? – Ian

回答

4

我不确定有一种方法可以在视图中做位置形状。然而,作为一种解决方法,我会考虑这样的事情。

<RelativeLayout > 
    <OldView with shape as background now without any background 
     android:layout_alignParentTop="true" android:layout_alignParentBottom="true"/> 
    <View with this shape as background 
     android:layout_alignParentBottom="true"/> 
</RelativeLayout> 

这应该会给你想要的东西。

+0

我已经决定,这是提交的人中最好的答案,它不依靠摆动蘸点或像素,它可以跨屏幕分辨率。谢谢。 –

+0

很高兴帮助! – PravinCG

+0

这仍然是最好的解决方法吗? –

0
I know gravity works on other kinds of drawables 

android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | 
          "fill_vertical" | "center_horizontal" | "fill_horizontal" | 
          "center" | "fill" | "clip_vertical" | "clip_horizontal"] 
+0

这似乎并没有在这里工作:/ –

2

下接收形状,这样就可以改变看法的利润率保证金布局PARAMS,反正它定位你想要的:

MarginLayoutParams params = (MarginLayoutParams)shape.getLayoutParams(); 
// Sets the margins of the shape, making it move to a specific location on the screen 
params.setMargins(int left, int top, int right, int bottom); 
1

我也有类似的问题,但我想有一个背景对于我的看法,一个矩形,但只显示矩形的左侧和底侧,没有顶部或右侧。为了实现这一点,我已经使用这个XML:

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

<item 
    android:bottom="2dp" 
    android:left="2dp" 
    android:right="2dp" 
    android:top="2dp"> 
    <shape 
     android:shape="rectangle" > 

     <stroke 
      android:width="1px" 
      android:color="#FFFF00" /> 
    </shape> 
</item> 
<item 
    android:bottom="3dp" 
    android:left="3dp" 
    android:right="1dp" 
    android:top="1dp"> 
    <shape 
     android:shape="rectangle" > 

     <stroke 
      android:width="1px" 
      android:color="#000000" /> 
    </shape> 
</item> 

注:有两个矩形,一个在另一个之上绘制,第二个第一个绘制在顶部,但有点偏移,所以1DP的第一个要显示在左侧和底部的屏幕上。此外,您必须注意,必须选择第二个颜色作为隐藏的颜色。在我的情况下,它是黑色的。

结果是(你可能只在左侧和底部观察黄线): Result example