1. 程式人生 > >使用一個shape.xml檔案,使用程式碼設定不同圓角背景顏色

使用一個shape.xml檔案,使用程式碼設定不同圓角背景顏色

          給一個View設定一個圓角的背景顏色,我們一般會使用xml檔案設定,使用<shape>節點設定,但是如果我們對一系列的View設定圓角北京,並且背景顏色色值不同,那麼我們第一感覺想到的是建立多個xml檔案,更改solid填充背景,其實我們可以使用一個xml檔案就可以搞定,使用程式碼更改裡面的填充顏色色值。廢話不多話,看程式碼。

        效果:

 首先:建立activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/gv_gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="10dp"
        android:numColumns="4"
        android:verticalSpacing="10dp" >
    </GridView>

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

	private GridView gdview;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = MainActivity.this;
		gdview = (GridView) findViewById(R.id.gv_gridview);
		initDate();
	}

	private void initDate() {
		List<String> lists = new ArrayList<String>();
		lists.add("顏色1");
		lists.add("顏色2");
		lists.add("顏色3");
		lists.add("顏色4");
		lists.add("顏色5");
		lists.add("顏色6");
		lists.add("顏色7");
		lists.add("顏色8");
		lists.add("顏色9");
		lists.add("顏色10");
		lists.add("顏色11");
		lists.add("顏色12");
		gdview.setAdapter(new MyGridViewAdapter(mContext, lists));
	}

}

下面新建自定義介面卡MyGridViewAdapter.java
public class MyGridViewAdapter extends BaseAdapter {

	private Context mContext;
	private List<String> data;// 顯示的資料
	private int[] colors = { R.color.item1, R.color.item2, R.color.item3,
			R.color.item4, R.color.item5, R.color.item6, R.color.item7,
			R.color.item8, R.color.item9, R.color.item10, R.color.item11,
			R.color.item12 };// 顏色色值id陣列

	public MyGridViewAdapter(Context mContext, List<String> data) {
		super();
		this.mContext = mContext;
		this.data = data;
	}

	@Override
	public int getCount() {
		if (data == null) {
			return 0;
		}
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_griview, null);
			holder.tv_item = (TextView) convertView.findViewById(R.id.tv_item);
			holder.tv_item.setBackgroundResource(R.drawable.circle);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		String item = (String) getItem(position);
		holder.tv_item.setText(item);
		// 獲取背景顏色,並且改變顏色
		GradientDrawable myGrad = (GradientDrawable) holder.tv_item
				.getBackground();
		myGrad.setColor(mContext.getResources().getColor(getColor(position)));
		return convertView;
	}

	/**
	 * 
	 * @方法名稱:getColor
	 * @描述: TODO
	 * @建立人:yang
	 * @建立時間:2014年9月30日 下午3:06:04
	 * @備註:獲取背景的顏色的色值
	 * @param position
	 * @return
	 * @返回型別:int
	 */
	public int getColor(int position) {
		if (position < colors.length) {
			return colors[position];
		} else {
			return colors[position % colors.length];
		}
	}

	class ViewHolder {
		TextView tv_item;
	}
}

item_griview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp" />

</LinearLayout>

提前在res/vlaues/color.xml中增加背景顏色色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="item1">#eed776</color>
    <color name="item2">#f2a96b</color>
    <color name="item3">#7fda54</color>
    <color name="item4">#aaaae1</color>
    <color name="item5">#d785de</color>
    <color name="item6">#a8a8e2</color>
    <color name="item7">#91d56c</color>
    <color name="item8">#d4ac65</color>
    <color name="item9">#7ac7c6</color>
    <color name="item10">#f09292</color>
    <color name="item11">#83b4e4</color>
    <color name="item12">#d7d87e</color>
</resources>


如果想顯示的背景的個數,背景顏色的色值和內容,可以在values中做相應的變化。

相關推薦

使用一個shape.xml檔案使用程式碼設定不同圓角背景顏色

          給一個View設定一個圓角的背景顏色,我們一般會使用xml檔案設定,使用<shape>節點設定,但是如果我們對一系列的View設定圓角北京,並且背景顏色色值不同,那麼我們第一感覺想到的是建立多個xml檔案,更改solid填充背景,其實我們可

Android 程式碼設定 控制元件背景顏色

直接看程式碼:    設定背景顏色兩種方式 RelativeLayout mRelativeLayout=(RelativeLayout) findViewById(R.id.Relative);         /**          * 1、在values目錄下新建

CSS-相關練習1-表格實現奇數行和偶數行自行判斷設定不同背景顏色

/* *Copyright (c) 2017,煙臺大學計算機學院 All rights reserved. *檔名稱:關於HTML的練習 *作 者:張晴晴 *完成日期:2017年11月25日 *版 本 號:v1.0 * *問題

定義一個xml檔案儲存班級資訊

xml檔案的功能:用來傳輸和儲存資料 案例: 編寫一個xml檔案,用來儲存班級的資訊,包括學生(姓名,年齡,城市,手機號)、老師(姓名,課程)、賬號資訊(賬號、密碼) 說明: 做文件宣告,宣告文件型別為xml,版本號,編碼 <?xml version="1.0" encodin

判斷本地系統目錄下是否存在XML檔案如果不存在就建立一個XMl檔案若存在就在裡面執行新增資料

這是我為專案中寫的一個測試的例子, 假如,您需要這樣一個xml檔案, <?xml version="1.0" encoding="utf-8"?> <A> <a> <id>001</id> <name>le

XStream生成XMl檔案設定別名

public class B2BPayToXml { public Head head; public Body body; public void setHead(Head head) { this.head = he

java w3c解析xml檔案獲取指定節點內容讀取外部配置檔案

原始碼: package com.ys.adage.utils; import com.ys.adage.message.CodeObjectResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.

1.使用dom4j解析xml檔案模擬伺服器解析web.xml

1. XML文件的構成 XML文件結構其實與html程式碼結構非常相似。 1. XML文件宣告 文件宣告必須以<?xml 開頭,以?>結束。 文件宣告必須從文件的0行0列開始。 文件宣告只有三個屬性: version:指定XML文件版本,必選,一般使用1.0

修改Windows下的hosts檔案以及設定普通使用者獲取管理員許可權

參考連結:https://jingyan.baidu.com/article/624e7459b194f134e8ba5a8e.html 首先進入Win10系統的hosts檔案所在位置,我們直接輸入C:\Windows\System32\Drivers\etc後回車就可以打開了,右鍵hosts檔

spring配置檔案ApplicationContext.xml檔案裡面程式碼沒有提示功能

  由於編者水平有限,文中難免會有錯誤和疏漏,請各位讀者能提出寶貴建議或給予指正,可在博文下評論指出,我會及時改進,在此先感謝各位。   本文是自己學習所做筆記,歡迎轉載,但請註明出處:http://blog.csdn.NET/yijuanxia. 系統環境:Win7-6

gecko 相關process分析 參考檔案程式碼

 NS_InitXPCOM2x重點分析:b2g process | |-NS_InitXPCOM2                       (xpcom/build/XPCOMInit.cpp)        |        |-sMessa

解析xml檔案修改Jenkins的配置

最近因為伺服器移動,在Jenkins中配置的一些地址之類的,都要改變,如圖,我因為使用外掛Sidebar Links增加一個連結地址,現在地址變了,所以在Jenkins中配置就需要改動link url的地址,如圖: 要改變成地址為:192.168.11.11,因為Jen

maven配置setting.xml檔案使用阿里雲下載地址

<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor licen

如何讀取xml檔案根據xml節點屬性查詢並輸出xml檔案

主要是應用SimpleXML和遞迴函式來根據key值來查詢,並將結果以xml格式輸出。 <?php header("Content-type: text/xml"); //以xml格式輸出檔案 @$key=$_GET['key']; $find=false; //echo $key."<

c# 操作xml檔案新增、刪除節點

/// <summary> /// 刪除當前選擇節點 /// </summary> /// <param name="sender"></param>

google test lcov genhtml 產生覆蓋率xml檔案去除不需要的檔案(include)或者包含需要的(source)

接我的上一篇 問題:在產生了.gcno 和 .gcda兩個檔案後,使用lcov -c -d Debug/source/ -o Debug/coverage.info 產生中間檔案coverage.info檔案,然後用genhtml -o output/  Debug/c

C 實現對XML檔案的基本操作(建立xml檔案增 刪 改 查 xml節點資訊)

                XML:Extensible Markup Language(可擴充套件標記語言)的縮寫,是用來定義其它語言的一種元語言,其前身是SGML(Standard Generalized Markup Language,標準通用標記語言)。它沒有標籤集(tag set),也沒有語法規

Spring整合Hibernate註解配置 無hibernate.cfg.xml檔案自動生成表配置

本以為一個無足掛齒的小問題,沒想到還折騰了一下。遂記錄一下。主要搜尋出的結果排名靠前的大多是在hibernate.cfg.xml中的配置方式。與我的環境不符。正確配置方式如下。已測試。 <bean id= "sessionFactor

Maven 專案pom.xml檔案完全程式碼

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.

maven的setiting.XML 檔案完成程式碼

設定的在window-Preferences-Maven-User Setting中,裡面是我的實際路徑,大家可以根據實際情況修改  第一種,不配代理的完整程式碼 <?xml version="1.0" encoding="UTF-8"?> <setting