2011-11-03 142 views

回答

11

如果您有ID为您的main.xml按钮= Button1的,那么你可以按如下方式使用它:

setContentView(R.layout.main); 

Button mButton=(Button)findViewById(R.id.button1); 
mButton.setTextColor(Color.parseColor("#FF0000")); // custom color 
//mButton.setTextColor(Color.RED); // use default color 
mButton.setBackgroundResource(R.drawable.button_shape); 

R.drawable.button_shape(button_shape.xml):

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#70ffffff" 
     android:centerColor="#70ffffff" 
     android:endColor="#70ffffff" 
     android:angle="270" /> 
    <corners 
     android:bottomRightRadius="8dp" 
     android:bottomLeftRadius="8dp" 
     android:topLeftRadius="8dp" 
     android:topRightRadius="8dp"/> 
</shape> 

你可以有你自己的形状file.change它根据你的需要。

1

基本上你必须遵循的方案:

1)获得参考你想改变

findViewById(R.id.<your_object_id>); 

2)将它转换为对象类型

Button btnYourButton = (Button) findViewById(R.id.<your_object_id>); 

3对象)使用setter对象“btnYourButton”

4)重绘您的视图(可能调用invali日期());

这取决于您何时想要发生更改。我假设你将有一个eventListener 附加到你的对象,并在事件被解雇后,你将执行你的改变。

4

你可以改变按钮的文字颜色动态像

按钮btnChangeTextColor =(按钮)findViewbyId(btnChange); btnChangeTextColor.setTextColor(Color.BLUE);

0

@覆盖 公共布尔的onTouchEvent(MotionEvent事件){

if (event.getAction() == MotionEvent.ACTION_DOWN) { 

     start_x = event.getX(); 
     start_y = event.getY(); 

    } else if (event.getAction() == MotionEvent.ACTION_MOVE) { 

     setTitle(event.getX() + "y pos" + event.getY()); 
     RelativeLayout layout = (RelativeLayout) findViewById(R.id.lay); 

     layout.setBackgroundColor(Color.rgb((int) start_x, (int) start_y, 0)); 
    } else if (event.getAction() == MotionEvent.ACTION_UP) { 



    } 
    return true; 
}