【问题标题】:How to round corners of TextView in Android (Java)? [duplicate]如何在Android(Java)中圆角TextView? [复制]
【发布时间】:2020-12-06 15:36:46
【问题描述】:

我的 Main-Activity 中有以下代码:

LinearesLayout = (LinearLayout) findViewById(R.id.linearlayout);
TextView textView = new TextView(this);
textView.setBackgroundResource(R.color.colorPrimary);
LinearesLayout.addView(textView);

如何将 TextView 的角设置为圆形?

【问题讨论】:

  • 您应该首先尝试自己找到一种方法,然后向我们展示您尝试过的方法,如果它不起作用并且您需要帮助了解原因。不要让别人为你工作
  • @TalMantelmakher 我已经尝试在 Internet 上找到它的命令。但是,如果我不知道该使用哪个命令,我该如何向您展示一些东西呢?
  • 它应该看起来像这样:textView.setradius="5dp"。但我找不到这样的想法......

标签: java android textview


【解决方案1】:

在 res\drawable 文件夹中创建一个 XML 文件 - rounded_corners.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#ff0000"/>
  <corners android:radius="4dp" />
  <padding
    android:top="5dp"
    android:left="5dp"
    android:bottom="5dp"
    android:right="5dp" />
</shape> 

替换这一行:

textView.setBackgroundResource(R.color.colorPrimary);

到这里:

textView.setBackgroundResource(R.drawable.rounded_corners);

【讨论】:

    猜你喜欢
    • 2013-09-17
    • 2016-03-30
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多