java - Android:创建一个圆形 TextView?

标签 java android textview xml-layout

下面是我当前的简单 XML,但是我希望其中的 3 个 TextView 是圆形的,而不是矩形的。

如何更改我的代码?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvSummary1"
        android:layout_width="270dp"
        android:layout_height="60dp" >
    </TextView>

    <TextView
        android:id="@+id/tvSummary2"
        android:layout_width="270dp"
        android:layout_height="60dp" >
    </TextView>

    <TextView
        android:id="@+id/tvSummary3"
        android:layout_width="270dp"
        android:layout_height="60dp" >
    </TextView>

</LinearLayout>

注意:我想要一个实际的圆形,而不是如下所示的弯曲边缘矩形:

enter image description here

编辑:

当前代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvSummary1"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:text=" " 
        android:gravity="left|center_vertical"
        android:background="@drawable/circle"/>

    <TextView
        android:id="@+id/tvSummary2"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:background="@drawable/circle" >
    </TextView>

    <TextView
        android:id="@+id/tvSummary3"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:background="@drawable/circle" >
    </TextView>

</LinearLayout>

当前输出:

enter image description here

最佳答案

我也在寻找解决这个问题的方法,并且我发现简单舒适的方法是将矩形 TextView 的形状转换为圆形。有了这个方法就完美了:

  1. 在名为“circle.xml”的drawable文件夹中创建一个新的XML文件(例如)并用以下代码填充它:

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

使用此文件,您将创建 TextView 的新形式。在这种情况下,我创建了一个绿色圆圈。如果要添加边框,则必须将以下代码添加到上一个文件中:

    <stroke
        android:width="2dp"
        android:color="#FFFFFF" />
  1. 使用以下代码在可绘制文件夹中创建另一个 XML 文件(“rounded_textview.xml”):

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/circle" />
    </selector>
    

这个文件将改变我们eligamos的TextView的方式。

  1. 最后,在我们要更改方式的 TextView 属性部分,我们前往“背景”并选择创建的第二个 XML 文件(“rounded_textview.xml”)。

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="H"
        android:textSize="30sp"
        android:background="@drawable/rounded_textview"
        android:textColor="@android:color/white"
        android:gravity="center"
        android:id="@+id/mark" />
    

通过这些步骤,TextView 不再是 TextView,而是矩形有一个圆形。只需更改形状,而不是 TextView 的功能。结果如下:

enter image description here

另外我不得不说,这些步骤可以应用于在属性中具有“背景”选项的任何其他组件。

好运!!

关于java - Android:创建一个圆形 TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25203501/

相关文章:

java - Lucene:新的 WordnetSynonymParser( boolean 去重、 boolean 展开、Analyzer 分析器)

Android 应用程序在 mixpanel 刷新之前关闭

android - ListView 在我开始新 Activity 后消失

java - 构造函数 TextView(new View.OnClickListener(){}) 未定义

java - 如何使用 IntelliJ Idea 15 调试 JSP 文件(断点不起作用)

java - Spring框架升级到5

java - 执行 sudo 命令时发现意外输出

java - 我的 MainActivity 类在扩展 ActionBarActivity 时抛出 java.lang.ClassNotFoundException

android - Android 中 txtview.settext 中的未知错误

c# - 安卓 C# TextView.setGravity