最近学习android开发,记录学习过程,分享一写小案例

一. 如下先设置好布局文件

<TextView
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="59dp"
        android:text="欢迎使用"
        android:textSize="40sp" />

    <Button
        android:
        android:onClick="clickRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="红"
       />

    <Button
        android:
        android:onClick="clickGreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="绿"
      />

    <Button
        android:
        android:onClick="clickBlue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignRight="@+id/textView1"
        android:text="蓝"
        />

  二 关键java代码 设置用getWindow().setBackgroundDrawable设置全局颜色

public void clickRed(View view) {
getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));
}

public void clickBlue(View view) {
getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLUE));

}

public void clickGreen(View view) {
getWindow().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
}

  运行效果 如下

android手电筒开发

 

 源码在这里 喜欢的拿去研究 点击下载

相关文章:

  • 2021-10-30
  • 2021-11-05
  • 2022-02-25
  • 2021-06-13
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2021-10-07
相关资源
相似解决方案