私はカスタマイズされた境界線を持つ円形のビューを持ち、CircularImageViewライブラリを使用して実装され、RecyclerView
で拡張されました。これまではイメージを入れていましたが、今はそのイメージを単色の丸とテキストに置き換えたいと考えています。既存の図形でソリッドカラーをプログラム的に変更する
私は、以下の形状(shape_colored_circle.xml
)を作成しました:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:height="70dp"
android:width="70dp"/>
<solid
android:color="#00f"/>
</shape>
そしてFrameLayout
でCircularImageView
と一緒TextView
を追加しました:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/card_thumbnail"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/icon"
app:civ_border="true"
app:civ_border_color="@color/border_color"
app:civ_border_width="2dp"
app:civ_shadow="true"
app:civ_shadow_color="@color/grey"
app:civ_shadow_radius="10"
android:contentDescription="@string/card_thumbnail"/>
<TextView
android:id="@+id/card_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/app_name"
android:textSize="18sp"
android:textStyle="bold"/>
</FrameLayout>
</LinearLayout>
私はレイアウトが少し奇妙に思えることを承知しています(なぜ「LinearLayout
のトップは必要なのですか?」)、それがなければRecyclerView
に表示されず、このレイアウト(図書館の例から取られています)が私たちのニーズに役立ちます。 onBindViewHolder
で
私はCircularImageView
リソースとして形状を設定します。
holder.thumbnail.setImageDrawable(activity.getResources().getDrawable(R.drawable.shape_colored_circle));
そして最後に、私は私が期待していたものを参照してください。問題は、私のdrawableがあらかじめ特定の色を設定していることです。したがって、私はプログラム的な形状を作成しようとしました:
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(colors[position]); //Getting the color from a predefined array of colors
holder.thumbnail.setImageDrawable(gd);
をしかし、ビューは単に黒い長方形に変わります。私は、形状をプログラムで作成するためのソリューションをいくつか試してみましたが(それらのすべてがかなり似ています)、無駄です。黒い矩形が表示されるか、まったく表示されません。
何か間違っていますか?実行時にシェイプを作成する別の方法、または既存のものを変更する方法はありますか?または、必要な動作を達成するための全く異なる方法でしょうか?
ありがとうございます!
を私はプログラムで色を設定しようとしたときに、私のdrawableの背景が黒くなってしまう問題が発生しました。多分似たような問題? http://stackoverflow.com/questions/24581900/android-button-background-xml-sometimes-loses-alpha-when-setting-color-filter – 0x5453
@ 0x5453 'gd.setAlpha()'を追加しました。値が255の場合は黒い四角形、値が0の場合は(明らかに)見えません。あなたのソリューションは、プログラムで作成されたドロウアブルと関連がありますか? –