【问题标题】:How to build trapezoid shape in xml android?如何在 xml android 中构建梯形?
【发布时间】:2018-03-12 02:02:46
【问题描述】:

我想用bottom linetext 在里面构建这个shape 我有点困惑如何实现这个我累了 一些代码,但没有得到必需的东西。

到目前为止,我已经尝试过这段代码

shape.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Colored rectangle-->
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="100dp"
                android:height="40dp" />
            <solid android:color="#13a89e" />
        </shape>
    </item>


    <!-- This rectangle for the right side -->
    <!-- Their color should be the same as layout's background -->
    <item
        android:right="-100dp"
        android:left="100dp"
        android:top="-100dp"
        android:bottom="-100dp">
        <rotate
            android:fromDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="#ffffff" />
            </shape>
        </rotate>
    </item>

</layer-list>

它提供以下结果。

我还需要这个形状下面的黄线。

感谢您的帮助。

【问题讨论】:

  • 看起来你快到了。在第一个矩形下方放置另一个矩形(高度较低)。为左侧制作另一个倾斜的矩形。使两个倾斜的矩形小一点。并对颜色做一些事情......
  • 我尝试在左侧放置相同的白色形状,但我不知道该怎么做
  • 这篇文章对我创建梯形视图有很大帮助arkapp.medium.com/trapezium-view-for-android-584799c7e849

标签: android android-layout android-shapedrawable


【解决方案1】:

我强烈建议创建自定义文本视图,这样它会更加灵活和可控。您需要创建路径对象,并使用该对象定义视图角。如果您想以文本形式查看,则需要覆盖 onDraw(Canvas canvas) 函数,您将调用 canvas.draw(path,paint) 方法。如果您不仅需要文本字段,您应该覆盖任何视图组类,但对于视图组,您应该覆盖 onDispatchDraw 函数来做到这一点。

你可以像下面的例子一样创建你的形状

    // you can define all points
    Point topLeft = new Point(0,0);
    Point topRight = new Point(getWidth(),0); // your view width
    //... 

    //cover your corner points
    Path path = new Path();
    path.moveTo(topLeft.x, topLeft.y);
    path.lineTo(topRight.x, topRight.y);
    path.lineTo(bottomRight.x, bottomRight.y);
    path.lineTo(shapeBottomRight.x, shapeBottomRight.y);
    path.lineTo(shapeTop.x, shapeTop.y);
    path.lineTo(shapeBottomLeft.x, shapeBottomLeft.y);
    path.lineTo(bottomLeft.x, bottomLeft.y);
    path.lineTo(topLeft.x, topLeft.y);

    canvas.draw(path, paint);

【讨论】:

  • 抱歉 gokhan 我不知道如何使用自定义视图,请您帮忙看看如何使用此代码。
  • @NoumanCh 为此,您可以创建自定义视图或视图组。为简单起见,您可以使用默认构造函数扩展框架布局。你需要重写 onDispatch 函数来绘制你想要的形状,就像我的例子一样。您可以尝试矩形的路径对象。定义四个角并使用绘画对象调用 canvas.draw(yourPath,paint),您可以使用等定义颜色边框。最后转到 xml 布局并放置您创建的布局,这就是您将看到的所有形状
  • 我需要把上面的代码放在onDispatch方法中吗?
  • @NoumanCh 如果您的视图组的视图类型(FrameLayout、LinearLayout ..)您将使用 onDispatch 或您的视图类型(Textview、ImageView ...)您将使用 onDraw
  • 您根本不需要自定义View,您需要的是自定义Drawable
【解决方案2】:

这是您的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- Colored rectangle-->
<item>
    <shape android:shape="rectangle">
        <padding android:top="35dp"/>
        <size android:width="200dp" android:height="40dp" />
        <solid android:color="#13a89e" />
    </shape>
</item>
<!-- Darker colored line-->
<item>
    <shape android:shape="line">
        <size android:width="100dp"/>
        <stroke android:width="4dp" android:color="#123456" />
    </shape>
</item>


<!-- This rectangle for the right side -->
<!-- Their color should be the same as layout's background -->
<item
    android:right="-200dp"
    android:left="200dp"
    android:top="-200dp"
    android:bottom="-200dp">
    <rotate android:fromDegrees="45">
        <shape android:shape="rectangle">
           <padding android:top="-35dp"/>
           <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>
<!-- This rectangle for the left side -->
<item
    android:right="200dp"
    android:left="-200dp"
    android:top="-200dp"
    android:bottom="-200dp">
    <rotate android:fromDegrees="-45">
        <shape android:shape="rectangle">
            <padding android:top="-35dp"/>
            <solid android:color="#ffffff" />
        </shape>
    </rotate>
</item>

这就是它呈现的内容:

这是我的TextView XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.kalabalik.tilted.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/box"
    android:text="Your Text!"
    android:textColor="#000000"
    android:gravity="center_horizontal|bottom"
    android:paddingBottom="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

【讨论】:

  • 我将它与 textview 一起用作背景,它没有显示任何内容。
  • 您之前以某种方式显示它,不是吗?
  • 对不起,我没听懂你说什么
  • 亲爱的告诉我如何在上面放文字
  • 它应该像你展示你自己的例子一样显示。
【解决方案3】:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="28dp"
android:height="5dp"
android:viewportWidth="800"
android:viewportHeight="304">
<path

    android:pathData="M215,41.4c0,0.2 -22.9,58.3 -51,129.1 -28,70.8 -51,129.8 -51,131.1l0,2.4 296.1,-0c281.6,-0 296.1,-0.1 295.6,-1.8 -0.2,-0.9 -11.9,-31 -25.9,-66.7 -14.1,-35.8 -36.9,-94.1 -50.9,-129.8l-25.3,-64.7 -193.8,-0c-106.6,-0 -193.8,0.2 -193.8,0.4z">
    <aapt:attr name="android:fillColor">
        <gradient
            android:endX="50"
            android:endY="99"
            android:startX="50"
            android:startY="1"
            android:type="linear">
            <item
                android:color="#FFFFFF"
                android:offset="0.0" />
            <item
                android:color="#AAA8A8"
                android:offset="0.8" />
            <item
                android:color="#AAA8A8"
                android:offset="1.0" />
        </gradient>
    </aapt:attr>
</path>

【讨论】:

    【解决方案4】:

    这篇文章对我创建梯形视图有很大帮助https://arkapp.medium.com/trapezium-view-for-android-584799c7e849

    <com.arkapp.trapeziumview.TrapeziumCustomView
            android:id="@+id/trapeziumCustomView"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:layout_margin="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:shapeColor="@color/teal_200"
            app:edgeRadius="18"
            app:slopeLength="88"
            app:slopeType="bottomRight" />
    

    在这个帮助下,我能够创建一个自定义视图,其中一条边作为斜坡

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多