activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvTtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.172"
android:textSize="17dp"
android:textColor="@color/myRed"
/>
<Button
android:id="@+id/btnSayHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:text="打招呼"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="260dp"
android:text="红色"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="320dp"
android:text="绿色"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="380dp"
android:text="蓝色"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
------------------------------------------------------------------
ColorActivity.java
package com.example.dfby.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ColorActivity extends AppCompatActivity implements View.OnClickListener{
private Button btnRed,btnGreen,btnBlue;
private TextView tvTtitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("我的日志","这是我的自定义Activity");
//设置有颜色
tvTtitle=findViewById(R.id.tvTtitle);
btnRed=findViewById(R.id.btnRed);//获取按钮对象
btnGreen=findViewById(R.id.btnGreen);//获取按钮对象
btnBlue=findViewById(R.id.btnBlue);//获取按钮对象
btnRed.setOnClickListener(this);
btnGreen.setOnClickListener(this);
btnBlue.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(this,"干嘛点击我",Toast.LENGTH_SHORT).show();
if(v.getId()==R.id.btnRed){
getWindow().setBackgroundDrawableResource(R.color.myRed);
}else if(v.getId()==R.id.btnGreen){
getWindow().setBackgroundDrawableResource(R.color.myGreen);
}else if(v.getId()==R.id.btnBlue){
getWindow().setBackgroundDrawableResource(R.color.myBlue);
}
}
}