如何在Android按钮上以编程方式设置drawableLeft?
我正在动态创建按钮。我首先使用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);
没有找到相关结果
已邀请:
10 个回复
迪擅哨乘傅
方法执行此操作。请参阅此处的示例。我在没有使用
的情况下使用了它并且它起作用了。你可以尝试任何一种方式。 更新:在此处复制代码会导致链接断开
要么
要么
香腔弥胯瓤
稀瓣囊
寇剩
郡晒景沧
抽法
所以你以前的所有drawable都将被保留。
桔马牛
补蹲农界维
唤副埂侧壬
使用下面的代码片段向按钮添加一个drawable:
。 •
当您使用Android矢量drawable并希望对21以下的API具有向后兼容性时,请将以下代码添加到: 在应用程序级别
:
在Application类中:
糕泰灌