val adapter = SparseArrayObjectAdapter()
adapter.set(ACTION_PLAY_VIDEO, Action(ACTION_PLAY_VIDEO.toLong(), resources.getString(R.string.play_asset)))
row.actionsAdapter = adapter
这是Android TV详细页面中的操作代码但是我不知道如何在xmls中以编程方式更改按钮文本颜色/按钮背景颜色?
为适配器提供演示者。
在Java中:
adapter.setPresenterSelector(new ClassPresenterSelector()
.addClassPresenter(Action.class, new Presenter() {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_button_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
Button button = viewHolder.view.findViewById(R.id.my_button_id);
//do stuff here, like
button.setBackgroundColor(Color.BLUE);
button.setTextColor(Color.RED);
//or
button.setOnFocusChangeListener((view, hasFocus) -> {
if (hasFocus) {
//change color?
}
});
}
@Override
public void onUnbindViewHolder(ViewHolder viewHolder) {
//required override
}
})
);