Android顏色配置器

来源:https://www.cnblogs.com/xqz0618/archive/2018/04/19/8882514.html
-Advertisement-
Play Games

一、Android Color設置 1、在xml文件中 想設置顏色直接設置background的屬性或者其他的color屬性。隨便設置一個顏色如#000,再點擊左邊的顏色方塊,彈出顏色選擇器選擇顏色 2、在java代碼中 ①Color.parseColor("#000"); 【提示】可以在佈局文件中 ...


一、Android Color設置

1、在xml文件中

想設置顏色直接設置background的屬性或者其他的color屬性。隨便設置一個顏色如#000,再點擊左邊的顏色方塊,彈出顏色選擇器選擇顏色

 

2、在java代碼中

①Color.parseColor("#000");

tvShow.setBackgroundColor(Color.parseColor("#000"));

【提示】可以在佈局文件中配置好顏色值,然後把用“#”表示的顏色帶到java代碼中用

 

②Color.BLACK 使用Color類自帶的顏色,不過都是一些基本色

tvShow.setBackgroundColor(Color.BLACK);

③定義Color資源文件,通過R.color.myColor引用

int color = R.color.myColor;
tvShow.setBackgroundResource(R.color.myColor);

 

④Color.argb(a,r,g,b)方法:

tvShow.setBackgroundColor(Color.argb(255, 255, 0, 0));

分別是alpha、紅(red)、綠(green)、藍(blue)四個顏色值(ARGB)。每個數字取值0-255,因此一個顏色可以用一個整數來表示。為了運行效率,Android編碼時用整數Color類實例來表示顏色。

【提示】通過此方法傳入對應的透明度值,紅色值,綠色值,藍色值進行顏色配置。因此我們可以通過此方法做一個簡單的顏色配置器。

 

二、顏色配置器案例

1、【效果】

界面設計的比較粗糙,希望大家能學到實現效果,優化界面。

2、【項目結構】

3、【代碼】

 ①activity_main.xml佈局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context=".MainActivity"
 7     android:orientation="vertical">
 8 
 9     <TextView
10         android:text="請輸入argb值:"
11         android:textSize="20sp"
12         android:textColor="#000"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:layout_margin="10dp"/>
16 
17     <LinearLayout
18         android:layout_width="match_parent"
19         android:layout_height="50dp"
20         android:orientation="horizontal">
21         <EditText
22             android:id="@+id/etA"
23             android:layout_width="0dp"
24             android:layout_weight="1"
25             android:layout_height="match_parent"
26             android:layout_margin="1dp"
27             android:hint="透明度(0-255)"
28             android:inputType="number"/>
29 
30         <EditText
31             android:id="@+id/etR"
32             android:hint="紅(0-255)"
33             android:layout_width="0dp"
34             android:layout_weight="1"
35             android:layout_height="match_parent"
36             android:background="#f00"
37             android:layout_margin="1dp"
38             android:gravity="center"
39             android:inputType="number"/>
40     </LinearLayout>
41     <LinearLayout
42         android:layout_width="match_parent"
43         android:layout_height="50dp"
44         android:orientation="horizontal">
45         <EditText
46             android:id="@+id/etG"
47             android:hint="綠(0-255)"
48             android:layout_width="0dp"
49             android:layout_weight="1"
50             android:layout_height="match_parent"
51             android:background="#0f0"
52             android:layout_margin="1dp"
53             android:gravity="center"
54             android:inputType="number"/>
55 
56         <EditText
57             android:id="@+id/etB"
58             android:hint="藍(0-255)"
59             android:layout_width="0dp"
60             android:layout_weight="1"
61             android:layout_height="match_parent"
62             android:background="#00f"
63             android:layout_margin="1dp"
64             android:gravity="center"
65             android:inputType="number"/>
66     </LinearLayout>
67 
68     <TextView
69         android:id="@+id/tv_show"
70         android:text="TextView"
71         android:layout_width="200dp"
72         android:layout_height="200dp"
73         android:background="#000"
74         android:layout_gravity="center"
75         android:layout_marginTop="20dp"
76         />
77 
78     <Button
79         android:id="@+id/btn"
80         android:text="確定配置"
81         android:layout_margin="20dp"
82         android:layout_width="match_parent"
83         android:layout_height="wrap_content" />
84 </LinearLayout>

【提示】EditText 中hint屬性:這是設置輸入框內的提示文字。 inputType屬性:設置輸入框輸入的文本類型,此處設置為整數型

②MainActivity.java文件

 1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 2 
 3     private EditText etA;
 4     private EditText etR;
 5     private EditText etG;
 6     private EditText etB;
 7     private TextView tvShow;
 8     private Button btn;
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14 
15         initView();
16     }
17 
18     private void initView() {
19         etA = (EditText) findViewById(R.id.etA);
20         etR = (EditText) findViewById(R.id.etR);
21         etG = (EditText) findViewById(R.id.etG);
22         etB = (EditText) findViewById(R.id.etB);
23         tvShow = (TextView) findViewById(R.id.tv_show);
24         btn = (Button) findViewById(R.id.btn);
25 
26         btn.setOnClickListener(this);
27     }
28 
29     @Override
30     public void onClick(View v) {
31         switch (v.getId()) {
32             case R.id.btn:
33                 submit();
34                 break;
35         }
36     }
37 
38     private void submit() {
39         // validate
40         if (!etA.getText().equals("")&&!etB.getText().equals("")
41                 &&!etR.getText().equals("")&&!etG.getText().equals("")) {
42             //對用戶輸入的數值進行判斷是否為空。避免空字元無法轉換為int異常
43             int et_a = Integer.parseInt(etA.getText().toString());
44             int et_r = Integer.parseInt(etR.getText().toString());
45             int et_g = Integer.parseInt(etG.getText().toString());
46             int et_b = Integer.parseInt(etB.getText().toString());
47             tvShow.setBackgroundColor(Color.argb(et_a, et_r, et_g, et_b));
48         }else {
49             Toast.makeText(this, "輸入的值不能為空", Toast.LENGTH_SHORT).show();
50         }
51     }
52 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 並行複製從庫發生自動重啟分析 背景 半同步複製從庫在晚上凌晨2點半發生自動重啟,另一個非同步複製從庫在第二天凌晨3點也發生了自動重啟。 分析 版本mysql 5.7.16 mysql show variables like '%slave_para%'; + + + | Variable_name | ...
  • Getting Started Getting Started. 1 1. Introduction. 1 2.Quick Start-Strandalone HBase. 1 2.1 JDK版本選擇... 1 2.2 Get Started With HBase. 1 2.3 偽分散式本地安裝.. ...
  • 正則表達式通常稱為regexes,是文本處理中模式匹配的一個標準,也是處理字元串的一個強有力的工具。使用正則表達式時,需要指定一個字元串作為模式串去檢索目標字元串。你可以使用正則表達式來查找字元串中匹配該正則表達式表示的模式的子串,也可以進行文本替換或者從目標文本中提取子串。 參考資料《iOS編程指 ...
  • 一個曲線 圖例: 多個曲線 圖例: ...
  • 一個簡單的界面: item.xml: 代碼: 開許可權: ...
  • 首先,我們都知道NSObject是大多數類的根類,但是,這個類的是怎麼實現的呢?我們可以去下載開源的Runtime源碼,探究下NSObject類的實現。 1. NSObject.h文件 我們可以直接使用Command點NSOject進去看到它的頭文件,可以看到,NSObject.h文件中有兩塊內容: ...
  • 前言: 這篇文章是我從事ISP研究數年來的一些經驗總結,沒有用到深奧的理論知識,有的只是根據實際狀況來處理的一些常規方法,以及曾經犯過的錯誤總結。我想把ISP function的原理用簡單淺顯的語言描述出來,希望對初學者有所幫助。這裡的ISP主要是指從CMOS sensor輸出的bayer patt ...
  • 先導入 然後在 info.plist 文件中添加 Privacy - Location Always and When In Use Usage Description 和 Privacy - Location When In Use Usage Description 兩個就可以了。 Locati ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...