如何在AndroidJava中修整TextView?

问题描述

我的主活动中包含以下代码

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

如何设置TextView的角为圆角?

解决方法

在res \ drawable文件夹-rounded_corners.xml中创建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);