我想在btn_presses,btn_focus等不同状态下更改按钮文字颜色。
为此,我使用了colorstatelist.xml,并在titlebarlayout.xml的按钮文本中引用了它。但我仍然无法改变按钮的文字颜色。
任何人都知道怎么做。我在代码的任何地方都会出错。
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
EditText emailEdit, passwordEdit;
Button loginButton;
String email, password;
TitleBarLayout titlebarLayout;
String r;
String rr;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
emailEdit = (EditText) findViewById(R.id.etEmail);
passwordEdit = (EditText) findViewById(R.id.etPassword);
loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(this);
// titlebarLayout=(TitleBarLayout) findViewById(R.id.titlebar);
titlebarLayout = new TitleBarLayout(MainActivity.this);
titlebarLayout.setLeftButtonText("");
titlebarLayout.setRightButtonText("Logout");
titlebarLayout.setTitle("iProtect");
//titlebarLayout.setLeftButtonSize(50,50);
//titlebarLayout.setRightButtonSize(100,50);
titlebarLayout.setLeftButtonBackgroundColor(Color.rgb(34,49,64));
titlebarLayout.setRightButtonBackgroundColor(Color.rgb(34,49,64));
titlebarLayout.setLeftButtonTextColor(Color.rgb(255,255,255));
titlebarLayout.setRightButtonTextColor(Color.rgb(255,255,255));
XmlResourceParser parser =getResources().getXml(R.color.colorstatelist);
ColorStateList colorStateList;
try {
colorStateList = ColorStateList.createFromXml(getResources(), parser);
titlebarLayout.setLeftButtonTextColor(colorStateList);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ColorStateList colorlist=new ColorStateList( new int[][] { new int[] { android.R.attr.state_focused }, new int[0], }, new int[] { Color.rgb(0, 0, 255), Color.BLACK, } );
// titlebarLayout.setLeftButtonTextColor(colorlist);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.left_button) {
} else if (v.getId() == R.id.right_button) {
}
}
};
titlebarLayout.setLeftButtonOnClickListener(listener);
titlebarLayout.setRightButtonOnClickListener(listener);
}
TitleBarLayout.java
public class TitleBarLayout {
private Activity activityRef;
private View contentView;
private Button leftButton, rightButton;
TextView titletext;
public TitleBarLayout(Activity a) {
Log.i("TitleBar Layout", "Inside constructor");
activityRef = a;
inflateViewsFromXml();
setListenersOnViews();
setValuesOnViews();
}
private void setValuesOnViews() {
leftButton.setText("");
rightButton.setText("");
}
private void setListenersOnViews() {
leftButton.setOnClickListener(listener);
rightButton.setOnClickListener(listener);
}
private final OnClickListener listener = (new OnClickListener() {
@Override
public void onClick(View v) {
activityRef.finish();
}
});
private void inflateViewsFromXml() {
contentView = activityRef.findViewById(R.id.titlebar);
rightButton = (Button) contentView.findViewById(R.id.right_button);
leftButton = (Button) contentView.findViewById(R.id.left_button);
titletext = (TextView) contentView.findViewById(R.id.title_textview);
}
public void setLeftButtonText(int resID) {
leftButton.setText(resID);
}
public void setLeftButtonText(String text) {
leftButton.setText(text);
}
public void setLeftButtonOnClickListener(View.OnClickListener listener) {
leftButton.setOnClickListener(listener);
}
public void setRightButtonText(int resID) {
rightButton.setText(resID);
}
public void setRightButtonText(String text) {
rightButton.setText(text);
}
public void setRightButtonOnClickListener(View.OnClickListener listener) {
rightButton.setOnClickListener(listener);
}
public void setTitle(int resID) {
titletext.setText("" + resID);
}
public void setTitle(String text) {
titletext.setText(text);
}
public void setLeftButtonSize(int width, int height) {
Log.i("Button" ,"Width"+width);
leftButton.setWidth(width);
leftButton.setHeight(height);
}
public void setRightButtonSize(int width, int height) {
Log.i("Button" ,"Width"+width);
rightButton.setWidth(width);
rightButton.setHeight(height);
}
public void setLeftButtonBackgroundResource(int backgroundResource) {
}
public void setRightButtonBackgroundResource(int backgroundResource) {
}
public void setLeftButtonBackgroundColor(int backgroundColor) {
Log.i("Button" ,"COLOR"+backgroundColor);
leftButton.setBackgroundColor(backgroundColor);
}
public void setRightButtonBackgroundColor(int backgroundColor) {
Log.i("Button" ,"COLOR"+backgroundColor);
rightButton.setBackgroundColor(backgroundColor);
}
public void setLeftButtonTextColor(int textcolor) {
Log.i("Button" ,"COLOR"+textcolor);
leftButton.setTextColor(textcolor);
}
public void setRightButtonTextColor(int textcolor) {
Log.i("Button" ,"COLOR"+textcolor);
rightButton.setTextColor(textcolor);
}
public void setLeftButtonTextColor(ColorStateList colorStateList) {
leftButton.setTextColor(colorStateList);
}
public void setRightButtonTextColor(ColorStateList colorStateList) {
}
}
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="title_bg_color">#2e4256</color>
<color name="layout_bg_color">#dcdcdc</color>
<color name="state_pressed">#ffffff</color>
<color name="state_selected">#00ff00</color>
<color name="state_focused">#0000ff</color>
</resources>
titlebarlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/titlebar_height"
android:background="@color/title_bg_color"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/left_button"
android:layout_width="@dimen/titlebar_button_width"
android:layout_height="@dimen/titlebar_button_height"
android:text=""
android:clickable="true"
android:textColor="@color/colorstatelist"
android:background="@drawable/button_shape_drawable"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/title_textview"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text=""
android:textColor="@android:color/white"
android:textStyle="bold" />
<Button
android:id="@+id/right_button"
android:layout_width="@dimen/titlebar_button_width"
android:layout_height="@dimen/titlebar_button_height"
android:text=""
android:clickable="true"
android:layout_marginRight="10dp"
android:background="@drawable/button_shape_drawable"
android:textColor="@color/colorstatelist" />
</LinearLayout>
colorstatelist.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="@color/state_pressed"/>
<item android:state_selected="true"
android:color="@color/state_selected"/>
<item android:state_focused="true"
android:color="@color/state_focused"/>
<item android:color="#808080"/>
</selector>
答案 0 :(得分:17)
首先,从代码中删除所有XML解析内容。所有它添加的是一些错误。你可以使用两种方法,你应该使用一种或另一种,而不是两种。
您的布局XML已将android:textColor
设置为您想要的状态列表。这就是你需要做的。按钮文本将根据其状态自动更改颜色。只需从您的Java代码中删除对setTextColor
的所有调用:目前,这些调用正在用静态颜色替换状态列表。我建议您从setLeft/RightButtonTextColor
中完全删除TitleBarLayout.java
方法,然后让IDE找到对它们的所有调用,并删除它们。
或者,如果要从Java代码而不是从XML设置颜色,只需要像下面这样调用它: -
button.setTextColor(getResources().getColorStateList(R.color.colorstatelist));
无需XML解析。