Dan*_*ana 3 css xml android draw
我是 Android 开发新手,我正在尝试在黄色relativelayout 内从左下角到右上角画一条线。我添加了一个layer-list-diagonal_line
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="300dp"
android:bottom="-300dp"
android:left="0dp"
android:right="-300dp">
<rotate
android:fromDegrees="-10"
android:pivotX="0%"
android:pivotY="100%" >
<shape
android:shape="line"
android:top="1dip" >
<stroke
android:width="1dip"
android:color="#000" />
</shape>
</rotate>
</item>
Run Code Online (Sandbox Code Playgroud)
然后到styles
<style name="diagonalStyle">
<item name="android:background">@drawable/diagonal_line</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后将其添加到我的RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
style="@style/diagonalStyle"
android:background="#FFDC7F">
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我添加颜色,则线条不会显示,如果没有颜色,线条会出现,但位置不正确。也许这个问题是重复的,但请温柔一点,我不知道我做错了什么。
您应该使用路径从左下角到右上角在VectorDrawable黄色内画线。RelativeLayout你的diagonal_line.xml应该是这样的(假设线条颜色是蓝色#0000FF,线条宽度是4):
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<path
android:name="diagonal_line"
android:strokeColor="#0000FF"
android:strokeWidth="4"
android:pathData="M600, 0 l-600, 600z" />
</vector>
Run Code Online (Sandbox Code Playgroud)
(绝对尺寸并不重要,因为矢量将根据大小重新缩放RelativeLayout)。您styles.xml应该包括部分
<style name="diagonalStyle">
<item name="android:background">@drawable/diagonal_line</item>
</style>
Run Code Online (Sandbox Code Playgroud)
正如您所写,如果您无法backgroundTint在文件中使用,您应该为您的{your_layout}.xml文件设置纯色背景( ),并在其上放置带有( )的“虚拟” 。像这样的东西:android:background="#FFDC7F"RelativelayoutViewdiagonalStylestyle="@style/diagonalStyle"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFDC7F"
tools:context="{YOUR_CONTEXT}">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/diagonalStyle" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
结果,你应该给出类似的东西:
更多路径教程在这里。
| 归档时间: |
|
| 查看次数: |
3596 次 |
| 最近记录: |