本文介绍了在RelativeLayout中从一角到另一角绘制对角线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,我试图在我的黄色RelativeLayout内部从左下角到右上角画一条线.我添加了layer-list-diagonal_line

I'm new in Android development and I'm trying to draw a line inside my yellow RelativeLayout from bottom left corner to top right corner. I've added a 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>

然后到styles

<style name="diagonalStyle">
    <item name="android:background">@drawable/diagonal_line</item>
</style>

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

我的问题是,如果我添加颜色,则线条不会显示,而没有颜色,线条会出现,但位置不正确.也许这个问题是重复的,但请保持温柔,我不知道我在做什么错.

My problem is that if I add the color the line is not showing and without color the line appears but is not in the correct position. Perhaps this question is a duplicate but please be gentle, I don't know what am I doing wrong.

推荐答案

您应使用 VectorDrawable 在黄色RelativeLayout内从左下角到右上角绘制线的路径.您的diagonal_line.xml应该像(假设线条颜色为蓝色#0000FF,线条宽度为4):

You should use VectorDrawable path to draw line inside yellow RelativeLayout from bottom left corner to top right corner. Your diagonal_line.xml should be like (assume line color is blue #0000FF and line width is 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>

(绝对尺寸并不重要,因为矢量将重新缩放为RelativeLayout大小).您的styles.xml应包含

(absolute dimensions not important, because vector will be rescaled for RelativeLayout size). Your styles.xml should include section

<style name="diagonalStyle">
    <item name="android:background">@drawable/diagonal_line</item>
</style>

如您所写,并且如果您无法在{your_layout}.xml文件中使用backgroundTint,则应为Relativelayout设置纯色背景(android:background="#FFDC7F"),并在diagonalStyle中放入虚拟" >(style="@style/diagonalStyle").像这样的东西:

as you wrote, and in case you cannot use backgroundTint in your {your_layout}.xml file, you should set solid colored background (android:background="#FFDC7F") for your Relativelayout and put "dummy" View with diagonalStyle (style="@style/diagonalStyle") over it. Something like that:

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

因此,您应该输入以下内容:

As result, you should give something like that:

更多路径教程此处

这篇关于在RelativeLayout中从一角到另一角绘制对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 16:20