Android 通过按钮更改矩形颜色
我正在尝试制作一个简单的应用程序,它可以显示矩形,并且可以通过按钮改变颜色 矩形类是:Android 通过按钮更改矩形颜色,android,Android,我正在尝试制作一个简单的应用程序,它可以显示矩形,并且可以通过按钮改变颜色 矩形类是: public class DrawView extends View{ Paint paint = new Paint(); public DrawView(Context context) { super(context); } public DrawView(Context context, AttributeSet attrs) { super(context, attrs); }
public class DrawView extends View{
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.YELLOW);
canvas.drawRect(300, 550, 150, 400, paint );
}
public void setColorRed()
{
paint.setColor(Color.RED);
invalidate();
}
我的应用程序是选项卡布局应用程序。该类在第三个选项卡中以以下方式显示:
main.xml
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bRedColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<com.thms.systemy3.DrawView
android:id="@+id/yourID"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.thms.systemy3.DrawView>
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
然后使用
drawview.setColorRed()
MainClass.java:
public class MainClass extends Activity implements OnClickListener{
TabHost th;
DrawView drawview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
th = (TabHost) findViewById(R.id.tabhost);
//tab3
Button bColorRed = (Button) findViewById(R.id.bRedColor);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("TAB1");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("TAB2");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("TAB3");
th.addTab(specs);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRedColor:
drawview.setColorRed();
break;
}
我做错了什么?有谁能纠正这个代码或给我一个适当的例子,设置一个简单的应用程序,绘制矩形,并能够改变一个按钮的颜色
谢谢你的回复 尝试使用以下方法:
paint.setStyle(Paint.Style.FILL);
paint.setStyle(Paint.Style.FILL);