[英]change background color for layout via programmatically in android
I have to develop one android application. 我必须开发一个Android应用程序。
I have to display the background color is #424340 for normal state.but i have to click these layout(selected and pressed state) means have to change the background color is #FF0000.How can i do programmatically...please give me solution for these... 我必须在正常状态下显示背景颜色为#424340。但是我必须单击这些布局(选择并按下状态)意味着必须更改背景颜色为#FF0000。我该如何编程...请给我解决方案对于这些...
I have used below code for creating layout programmtically: 我使用以下代码以编程方式创建布局:
LinearLayout ar = new LinearLayout(this);
ar.setOrientation(LinearLayout.VERTICAL);
ar.setPadding(3, 3, 3, 3);
ar.setLayoutParams(artiLayoutParams);
ar.setGravity(Gravity.CENTER);
ar.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_selector));
Here i ahve to click these layout means change the background color.so i ahve using below code.please check it. 在这里我点击这些布局意味着改变背景颜色。所以我使用下面的代码。请检查一下。
list_selector.xml: list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:state_pressed="false"
android:state_focused="false"
android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
android:state_selected="true"
android:state_focused="true"
android:drawable="@drawable/gradient_bg_hover" />
</selector>
gradient_bg.xml: gradient_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="3dp" android:color="#424340" />
<padding android:left="3dp" android:top="3dp" android:right="3dp"
android:bottom="3dp" />
<corners android:radius="4dp" />
</shape>
gradient_bg_hover.xml: gradient_bg_hover.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="3dp" android:color="#FF0000" />
<padding android:left="3dp" android:top="3dp" android:right="3dp"
android:bottom="3dp" />
<corners android:radius="4dp" />
</shape>
But my background color is didn't change.Whats wrong in my code.please help me... 但是我的背景颜色没有改变。我的代码有什么问题。请帮助我...
Your states seem wrong. 您的状态似乎不对。 Can you try this to confirm if it works? 您可以尝试一下以确认是否有效吗?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_pressed="false"
android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="@drawable/gradient_bg_hover" />
</selector>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.