第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Android按鈕上以編程方式設置drawableLeft?

如何在Android按鈕上以編程方式設置drawableLeft?

陪伴而非守候 2019-10-05 11:11:20
我正在動態(tài)創(chuàng)建按鈕。我首先使用XML設置了樣式,然后嘗試使用下面的XML并將其編程。<Button    android:id="@+id/buttonIdDoesntMatter"    android:layout_height="wrap_content"    android:layout_width="fill_parent"    android:text="buttonName"    android:drawableLeft="@drawable/imageWillChange"    android:onClick="listener"    android:layout_width="fill_parent"></Button>到目前為止,這就是我所擁有的。除了可繪制對象,我可以做所有事情。linear = (LinearLayout) findViewById(R.id.LinearView);Button button = new Button(this);button.setText("Button");button.setOnClickListener(listener);button.setLayoutParams(    new LayoutParams(        android.view.ViewGroup.LayoutParams.FILL_PARENT,                 android.view.ViewGroup.LayoutParams.WRAP_CONTENT    ));      linear.addView(button);
查看完整描述

3 回答

?
狐的傳說

TA貢獻1804條經(jīng)驗 獲得超3個贊

您可以使用該setCompoundDrawables方法來執(zhí)行此操作。請參閱此處的示例。我使用了此功能,但未使用,setBounds并且效果良好。您可以嘗試任何一種方式。


更新:如果鏈接斷開,請在此處復制代碼


Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );

img.setBounds( 0, 0, 60, 60 );

txtVw.setCompoundDrawables( img, null, null, null );

要么


Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );

txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);

要么


txtVw.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);


查看完整回答
反對 回復 2019-10-05
?
天涯盡頭無女友

TA貢獻1831條經(jīng)驗 獲得超9個贊

我這樣做:


 // Left, top, right, bottom drawables.

            Drawable[] drawables = button.getCompoundDrawables();

            // get left drawable.

            Drawable leftCompoundDrawable = drawables[0];

            // get new drawable.

            Drawable img = getContext().getResources().getDrawable(R.drawable.ic_launcher);

            // set image size (don't change the size values)

            img.setBounds(leftCompoundDrawable.getBounds());

            // set new drawable

            button.setCompoundDrawables(img, null, null, null);


查看完整回答
反對 回復 2019-10-05
?
aluckdog

TA貢獻1847條經(jīng)驗 獲得超7個贊

Kotlin Version

使用以下代碼片段向按鈕添加一個可繪制對象:


val drawable = ContextCompat.getDrawable(context, R.drawable.ic_favorite_white_16dp)

button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)


? Important Point in Using Android Vector Drawable

當您使用可繪制的android vector并希望向21以下的API向后兼容時,請將以下代碼添加到:


在應用程序級別build.gradle:


android {

    defaultConfig {

        vectorDrawables.useSupportLibrary = true

    }

}

在應用程序類中:


class MyApplication : Application() {


    override fun onCreate() {

        super.onCreate()


        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)

    }


}


查看完整回答
反對 回復 2019-10-05
  • 3 回答
  • 0 關注
  • 759 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號