2016-12-27 2 views
0

, как показано ниже в коде, у меня есть некоторые виды, и я хочу добавить цвет селектора в представление, чтобы при щелчке на экране его цвет изменился. точно так же, как это происходит, когда один клик по элементу в списке, вот что я делаю .Как добавить селекторный цвет к виду программно

я называю некоторые должности и я troed оба из следующих:

view.setBackgroundResource(android.R.attr.listChoiceBackgroundIndicator);//highlighted with red color 
view.setBackground(android.R.attr.listChoiceBackgroundIndicator);//highlighted with red color 

, но он не работает и AndroidStudio выделить их красным цветом.

пожалуйста, дайте мне знать, как добавить цвет селектора в моих соперничает программно

код:

LayoutInflater inflator = this.getLayoutInflater(); 
    final View view = inflator.inflate(R.layout.versicherungs_docs_footer, null); 

    RelativeLayout relLay = (RelativeLayout) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_mainContainer); 
    final TextView texVieShowMore = (TextView) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_texVie_showMore); 
    final TextView texVieShowLess = (TextView) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_texVie_showLess); 
    final TextView texVieShowMoreArrow = (TextView) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_texVie_showMoreArrow); 
    final TextView texVieShowLessArrow = (TextView) view.findViewById(R.id.versicherungslisteactivity2_meinedocs_lisvie_footer_texVie_showLessArrow); 

    view.setBackgroundResource(android.R.attr.listChoiceBackgroundIndicator);//highlighted with red color 
    //view.setBackground(android.R.attr.listChoiceBackgroundIndicator);//highlighted with red color 
+0

Итак, вы хотите изменить фон элемента после щелчка? (и по пункту я ссылаюсь на listItem) –

+0

да, я хочу изменить цвет фона после clciked..and мои взгляды в опубликованном коде, являются нормальными представлениями, как вы можете видеть .. это nt listview – user2121

+0

it должен работать с setBackgroundResource, можете ли вы добавить полный пример? – petrumo

ответ

0

Вы должны получить вытяжке, что указывает на android.R.attr.listChoiceBackgroundIndicator, как предлагается в этом link и устанавливает его программно.

// Create an array of the listChoiceBackgroundIndicator attribute 
int[] attrs = new int[] { listChoiceBackgroundIndicator /* index 0 */}; 

// Obtain the styled attributes. 'themedContext' is a context with a 
// theme, typically the current Activity (i.e. 'this') 
TypedArray ta = themedContext.obtainStyledAttributes(attrs); 

// To get the value of the 'listChoiceBackgroundIndicator' attribute that was 
// set in the theme used in 'themedContext'. The parameter is the index 
// of the attribute in the 'attrs' array. The returned Drawable 
// is what you are after 
Drawable drawableFromTheme = ta.getDrawable(0 /* index */); 

// Then, free the resources used by TypedArray 
ta.recycle(); 

// Finally, set drawable as background 
view.setBackground(drawableFromTheme); 
+0

Я пробовал ваш ответ, на самом деле я не работал, нет эффекта, когда нажимается на просмотр, кроме того, он доступен из API 16, пока я нацелен на API 15 – user2121

Смежные вопросы