私のプロジェクトでは、実行時に円を動的に描画する必要があります。そのため、プログラムで円を作成するために ShapeDrawable を使用していますが、残念ながら、CircleShape の ShapeDrawable 内にクラスやメソッドが見つかりませんでしOvalShape()
た。円の直径または半径を渡すだけで、ShapeDrawableを介して円を描くのを手伝ってください。前もって感謝します。あらゆる種類のカスタマイズは、ソリューションを修正するのに役立ちます。
私が ShapeDrawable に使用しているコードは
public static ShapeDrawable drawCircle (Context context, int width, int height, int color) {
//////Drawing oval & Circle programmatically /////////////
ShapeDrawable oval = new ShapeDrawable (new OvalShape ());
oval.setIntrinsicHeight (height);
oval.setIntrinsicWidth (width);
oval.getPaint ().setColor (color);
return oval;
}
で使用するコードMainActivity.java
if(Build.VERSION.SDK_INT >= 16) {
txtCount.setBackground (Util.drawCircle (MainActivity.this, 50, 50, getResources ().getColor (R.color.yellow)));
txtHotelCount.setText ("20");
}else{
txtCount.setBackgroundDrawable (Util.drawCircle (MainActivity.this, 50, 50, getResources ().getColor (R.color.yellow)));
txtHotelCount.setText ("20");
}
私のプロジェクトでTextView に使用するxmlはtxtCount
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@color/white">
<TextView
android:id="@+id/txt_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_grey"
android:gravity="center"
android:textSize="12sp"
android:padding="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_grey"
android:text="AVAILABLE"
android:layout_marginLeft="10dp"
android:gravity="center"
/>
</LinearLayout>
しかし、同じ幅と高さを 50 に設定した後でも、まだうまくいきません。プロパティはまだ楕円形のように動作しています。