TextView の左の描画可能オブジェクトをプログラムで設定する 質問する

TextView の左の描画可能オブジェクトをプログラムで設定する 質問する

ここには xml の textView があります。

<TextView
        android:id="@+id/bookTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableLeft="@drawable/checkmark"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="24dip"
        android:maxLines="1"
        android:ellipsize="end"/>

ご覧のとおり、XML で DrawableLeft を設定しました。

コード内でdrawableを変更したいと思います。

これを実行する方法はありますか? または、テキスト ビューのコードで drawableLeft を設定しますか?

ベストアンサー1

使用できますsetCompoundDrawablesWithIntrinsicBounds(int 左、int 上、int 右、int 下)

画像を表示したくない場合は0を設定します

左側のDrawableの例:

TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

あるいは、複合描画可能オブジェクト相対値を設定するRTL/LTRレイアウトを尊重します。


ヒント: XML 属性はわかっているが、実行時にそれをどのように使用すればよいか分からない場合は、開発者ドキュメントでそのプロパティの説明を参照してください。実行時にサポートされている場合は、関連メソッドが見つかります。例:DrawableLeftの場合

おすすめ記事