如何以编程方式更改Android TV操作按钮文本的颜色?

时间:2019-03-15 17:24:01

标签: android-tv leanback

val adapter = SparseArrayObjectAdapter()
    adapter.set(ACTION_PLAY_VIDEO, Action(ACTION_PLAY_VIDEO.toLong(), resources.getString(R.string.play_asset)))
    row.actionsAdapter = adapter

这是我在AndroidTV的DetailPage中执行操作的代码 但是我不知道如何以编程方式而不是在xmls中更改按钮文本颜色/按钮背景颜色?

1 个答案:

答案 0 :(得分:0)

为适配器提供演示者。

在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
        }

    })
);