简体   繁体   English

如何使用xml(Android)中的可绘制形状绘制半椭圆形状

[英]How to draw a semi-oval shape using shape drawable in xml (Android)

椭圆形的Android布局

I am struck at this problem of creating the UI in android. 我对在Android中创建UI的这个问题感到震惊。 I need a xml-based solution to this problem. 我需要一个基于xml的解决方案来解决这个问题。 I don't want to use any SVG or PNG image to achieve the solution. 我不想使用任何SVG或PNG图像来实现解决方案。 Is there any way to achieve this of view in android. 有没有办法在android中实现这个视图。

You should be using a <layer-list> element of xml to draw such a UI. 您应该使用xml的<layer-list>元素来绘制这样的UI。 Following code will be used to create oval shape: 以下代码将用于创建椭圆形:

<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#AAAAAA"></solid>
<size android:height="100dp"
    android:width="60dp">
</size>

This leads to the following shape: 这导致以下形状:

在此输入图像描述

Once you know how to draw an oval shape using xml. 一旦你知道如何使用xml绘制椭圆形状。 Now next step for you is to learn how to use <layer-list> to achieve desired output. 现在,下一步是学习如何使用<layer-list>来实现所需的输出。 To learn using <layer-list> you can follow this tutorial. 要学习使用<layer-list>您可以按照教程进行操作。

By creating a new drawable file which will describe the look of the widget you want to draw,more or less.You can then set it as background for which widget you want to change the original look of. 通过创建一个新的可绘制文件来描述您想要绘制的小部件的外观,或多或少。然后您可以将其设置为要更改其原始外观的小部件的背景。 I would recommend starting with rectangle if you want to achieve the shape shown in your question. 如果你想达到你问题中显示的形状,我建议从矩形开始。 the drawable file will look something like this: drawable文件看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="#000"/>

    <corners android:radius="150dp"/> <!-- The value here should specify How much ovally you want shape be -->
</shape>

and then assigning the background of the widget to this drawable should do the trick, something like this: 然后将窗口小部件的背景分配给这个drawable应该做的伎俩,如下所示:

 <LinearLayout
    android:background="@drawable/oval">

Hope this was helpful :D 希望这有用:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM