【问题标题】:How to create transparent semicircle? [closed]如何创建透明的半圆? [关闭]
【发布时间】:2013-11-17 20:40:33
【问题描述】:

我想创建这个效果。TextView 以透明半圆为背景。 我尝试添加可绘制作为背景。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <solid android:color="#00000000" />
</shape>

我也试过这个stackoverflow similar question,但它对我不起作用。 谁能帮帮我?

【问题讨论】:

  • 您需要具体说明您的问题。它看起来不像您的意图(如果不是,请解释看起来有什么问题)?您的意思是您的应用程序崩溃(如果是,请显示 logcat 以便诊断)?阅读 Asking A Good Question 上的 stackoverflow 帮助
  • 不,我的应用程序没有崩溃。静默不起作用。

标签: android user-interface view drawable


【解决方案1】:

您不能从 xml 创建半圆。但是您可以使用具有适当边距和填充的圆圈来实现您正在寻找的东西。

像这样创建一个固定大小的圆圈:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false" >
    <solid android:color="#006AC5" />
    <size
        android:height="50dp"
        android:width="50dp" />
</shape>

现在将它用作 TextView 的背景,如下所示:

<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="#FFFF00">

    <ImageView
        android:contentDescription="@string/app_name"
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="200dip"
        android:layout_alignParentTop="true"
        android:background="#006AC5" />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/img"
        android:layout_marginTop="-35dip"
        android:paddingTop="10dip"
        android:background="@drawable/circle"
        android:layout_centerHorizontal="true"
        android:text="@string/one"
        android:gravity="center"/>

</RelativeLayout>

现在,如果您为 TextView 提供 -25dip(圆圈高度的一半)的上边距,您将在底部得到一个完美的半圆,但我只添加了另一个 10dip(和 10dip 填充以保留文本到位)只是为了更仔细地查看您要查找的内容。

如果您还想更改顶部 ImageView 的背景并且您不希望圆圈的其余部分弄乱外观,您可以在添加 TextView 后添加 ImageView,这样它就有更高的 z- index,然后使用单独的 TextView 在 Image 上方显示文本。我知道这很讨厌,但它应该可以工作:)

【讨论】:

  • 如果imageView有随机图片作为背景呢?
  • 您只需在添加 TextView 后将其添加到 xml 中(这样它就会放在文本上方),然后在 TextView 中添加一些额外的顶部填充,以便您可以看到文本。这可能是这样的:
猜你喜欢
  • 2014-10-10
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
相关资源
最近更新 更多