[英]gradient bar with line
我正在尝试获取图像中的梯度条,但我如何在梯度条上以25%,50%的比例获得白线
<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:width="1dp"
android:color="#2B2B2D" >
</solid>
<stroke
android:width="1dp"
android:color="#666666" >
</stroke>
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" >
</padding>
<corners
android:radius="2dp" >
</corners>
</shape>
</item>
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<corners
android:radius="2dp" >
</corners>
<gradient
android:angle="180"
android:endColor="#11B24A"
android:centerColor="#fec804"
android:startColor="#E3434B"
android:type="linear" />
</shape>
</item>
</layer-list>
我使用上面的代码作为imageview的背景,我可以使用高度为8dp和宽度为1dp的视图获得线条,我如何将它们精确地放置在25%50%的位置。
我有一个hacky的方式来做到这一点:
代码:
gradient_bar.xml(与您的相同)
<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:width="1dp"
android:color="#2B2B2D" >
</solid>
<stroke
android:width="1dp"
android:color="#666666" >
</stroke>
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" >
</padding>
<corners
android:radius="2dp" >
</corners>
</shape>
</item>
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<corners
android:radius="2dp" >
</corners>
<gradient
android:angle="180"
android:endColor="#11B24A"
android:centerColor="#fec804"
android:startColor="#E3434B"
android:type="linear" />
</shape>
</item>
</layer-list>
white_line.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
<View
android:layout_width="1dp"
android:layout_margin="2dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:background="@android:color/white"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> <View android:layout_width="1dp" android:layout_margin="2dp" android:layout_height="match_parent" android:layout_alignParentEnd="true" android:background="@android:color/transparent"/> </RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <View android:layout_width="match_parent" android:layout_height="10dp" android:background="@drawable/gradient_bar" android:layout_centerVertical="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="10dp" android:orientation="horizontal" android:layout_centerVertical="true"> <include layout="@layout/white_line"/> <include layout="@layout/white_line"/> <include layout="@layout/white_line"/> <include layout="@layout/white_line"/> <include layout="@layout/no_white_line"/> </LinearLayout> </RelativeLayout>
输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.