Android 在相对线框内从一个角到另一个角画对角线

Android 在相对线框内从一个角到另一个角画对角线,android,css,xml,draw,Android,Css,Xml,Draw,我是Android开发的新手,我正试图在我的黄色RelativeLayout中从左下角到右上角画一条线。 我添加了一个层列表-对角线 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:top="300dp" android:bottom="-300dp" android:left="0dp" android:right="-3

我是Android开发的新手,我正试图在我的黄色RelativeLayout中从左下角到右上角画一条线。 我添加了一个
层列表
-
对角线

<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>
然后将其添加到我的
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">

我的问题是,如果我添加了颜色,线条就不会显示,如果没有颜色,线条就会出现,但位置不正确。也许这个问题是重复的,但请温柔一点,我不知道我做错了什么。

您应该使用路径在黄色
相对线框内从左下角到右上角画一条线。您的
对角线.xml
应该如下(假设线条颜色为蓝色
\0000FF
且线条宽度为
4
):

在编写时,如果您不能在
{your_layout}.xml
文件中使用
backgroundTint
,您应该为您的
相对视图设置纯色背景(
android:background=“#FFDC7F”
),并使用
diagonalStyle
style=“@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>

因此,您应该给出如下内容:

<?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>


更多路径教程。

在相对布局中使用
android:background=“#FFDC7F”
而不是
android:backgroundTint=“#FFDC7F”
。我无法使用backgroundTint,而是找到了添加背景的解决方案,我在对角线xml中添加了一个新项。但线路位置仍然是错误的。我不知道如何正确定位:(
<style name="diagonalStyle">
    <item name="android:background">@drawable/diagonal_line</item>
</style>
<?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>