Java 如何在视图中定位自定义形状?
我在res/drawable文件夹中定义了一个自定义形状:Java 如何在视图中定位自定义形状?,java,android,Java,Android,我在res/drawable文件夹中定义了一个自定义形状: <?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="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>
我用它作为我的一个观点的背景。形状在视图中垂直居中放置,但我希望它显示在视图的底部,有没有办法做到这一点?我不确定有没有办法在视图中放置形状。然而,作为一种解决方法,我会考虑这样的事情。
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"]
<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>
这应该会给你你想要达到的目标。我不确定是否有办法在视图中定位形状。然而,作为一种解决方法,我会考虑这样的事情。
<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>
这应该会给你你想要达到的目标。试试这个,这可能很有用 将自定义视图设置为布局或视图的背景。
或者,如果没有用,请在此处提供布局xml文件代码,以便我可以更轻松地帮助您 试试这个,这可能有用 将自定义视图设置为布局或视图的背景。
或者,如果没有用,请在此处提供布局xml文件代码,以便我可以更轻松地帮助您 以下程序将接收形状的边距布局参数,以便您可以更改视图的边距,并根据需要对其进行定位:
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);
以下内容接收形状的边距布局参数,以便您可以更改视图的边距,并根据需要对其进行定位:
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);
我有一个类似的问题,但我希望我的视图有一个背景,一个矩形,但只显示矩形的左侧和底部,没有顶部或右侧。为了实现这一点,我使用了以下xml:
<?xml version="1.0" encoding="utf-8"?>
注意:有两个矩形,一个画在另一个的上面,第二个画在第一个的上面,但是有点移位,因此第一个矩形的1dp将显示在屏幕的左侧和底部。另外,您必须注意,第二个的颜色必须选择为隐藏的颜色。就我而言,它是黑色的
结果是(您可能只看到左侧和底部的黄线):
我也遇到了类似的问题,但我希望视图有一个背景,一个矩形,但只显示矩形的左侧和底部,没有顶部或右侧。为了实现这一点,我使用了以下xml:
<?xml version="1.0" encoding="utf-8"?>
注意:有两个矩形,一个画在另一个的上面,第二个画在第一个的上面,但是有点移位,因此第一个矩形的1dp将显示在屏幕的左侧和底部。另外,您必须注意,第二个的颜色必须选择为隐藏的颜色。就我而言,它是黑色的
结果是(您可能只看到左侧和底部的黄线):
我决定嵌入块
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#f00" />
<corners android:radius="50dp" />
<padding android:bottom="20dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#090" />
<corners android:radius="50dp" />
<padding
android:top="450dp"
/>strong text
</shape>
</item>
</layer-list>
强文本
我决定嵌入块
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#f00" />
<corners android:radius="50dp" />
<padding android:bottom="20dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#090" />
<corners android:radius="50dp" />
<padding
android:top="450dp"
/>strong text
</shape>
</item>
</layer-list>
强文本
我认为这是提交的答案中最好的一个,它不依赖于调整倾斜或像素,而且可以跨屏幕分辨率工作。谢谢。我已经决定,这是其中提交的最好的答案,它不依赖于摆弄倾角或像素,它可以跨屏幕分辨率工作。非常感谢。