我有一个linearlayout,如下面的code1所示,我添加了一个背景,以便在选择时更改其外观/背景。
在下面的代码2中,我给视图充气,我想添加相同的选择器/背景' android:background ="?android:attr / listChoiceBackgroundIndicator"' 到code2中膨胀的视图
我该如何以编程方式执行此操作?
CODE1 :
<LinearLayout
android:id="@+id/versicherungs_verDetailsListFooter_container"
android:layout_width="match_parent"
android:layout_height="@dimen/vers_height"
android:layout_below="@id/horizontalDivider0"
android:clickable="true"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:visibility="gone"
android:weightSum="1"
android:background="?android:attr/listChoiceBackgroundIndicator">
码2 :
final LinearLayout linLay = (LinearLayout) findViewById(R.id.versicherungs_verDetailsListFooter_container);
LayoutInflater inflator = this.getLayoutInflater();
final View view = inflator.inflate(R.layout.versicherungs_docs_footer, null);
RelativeLayout relLay = (RelativeLayout) view.findViewById(R.id.versicherungs_footer_mainContainer);
final TextView texVieShowMore = (TextView) view.findViewById(R.id.showMore);
final TextView texVieShowLess = (TextView) view.findViewById(R.id.showLess);
final TextView texVieShowMoreArrow = (TextView) view.findViewById(R.id.showMoreArrow);
final TextView texVieShowLessArrow = (TextView) view.findViewById(R.id.showLessArrow);
//how to add the selector "?android:attr/listChoiceBackgroundIndicator" to the RelativeLayout "relLay"
答案 0 :(得分:1)
您需要解决主题中的属性:
TypedValue typedValue = new TypedValue();
if (getTheme() != null) {
getTheme().resolveAttribute(R.attr.listChoiceBackgroundIndicator, typedValue, true);
}
int drawableResourceId = typedValue.resourceId; // it can by equal 0
然后设置视图的后台资源:
relLay.setBackgroundResource(drawableResourceId);