Android(Java)でTextViewの角を丸める方法は?[複製]

Aug 17 2020

Main-Activityに次のコードがあります。

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

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);