Как скруглить углы TextView в Android (Java)? [дубликат]

Aug 17 2020

В моем основном действии есть следующий код:

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

Как я могу сделать углы TextView закругленными?

Ответы

TalMantelmakher Aug 17 2020 at 22:40

Создайте XML-файл в папке res \ drawable - 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);