android - 以编程方式设置按钮样式

标签 android button programmatically-created

我正在尝试以编程方式创建带有按钮和微调器的布局。我不能在 xml 中执行此操作,因为微调器的数量取决于某些参数。

我的问题是:如何从@style 资源设置按钮样式? 它只需要在创建布局时设置一次。

我创建按钮的代码(类:DBsetupLayout):

RelativeLayout.LayoutParams doneButtonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    doneButton = new Button(this.context);
    doneButtonParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
    doneButton.setLayoutParams(doneButtonParams);
    doneButton.setText("DONE");
    doneButton.setId(2);

我还没有找到任何添加样式的addRule选项。

MainActivity 包含以下代码:

DBsetupLayout setupLayout = new DBsetupLayout(this, activityStatesNumber);
btdevSetupSaveButton = setupLayout.doneButton;
btdevSetupSaveButton.setOnClickListener...

我试过这个:android set style in code ,但它对我不起作用,所以我正在寻找某种其他解决方案。

感谢任何帮助。

问候

最佳答案

可以设置按钮背景。

使用此代码。

button.setBackgroundResource(R.drawable.new_button_drawable);

关于android - 以编程方式设置按钮样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26926786/

相关文章:

android - CoordinatorLayout 与 NestedScrollView 和水平滚动 RecyclerView

java - Android如何使用retrofit传递模态类和参数数组

android - 我怎样才能将项目从 eclipse 导入到 android studio

button - 在 Swift 中更改 IBOutlet 的位置

android - BluetoothAdapter.ACTION_REQUEST_ENABLE 立即调用 onActivityResult

javascript - 用结果替换加载提示的按钮

javascript 按钮 onclick 只按下一次

objective-c - 以编程方式容器 View Controller

java - 使用 if() 以编程方式向relativelayout添加不同的按钮

android - 始终在 Horizo​​ntalScrollView 中显示 3 个按钮