格局中@null的代碼實現方式
佈局中通常會用到@null。如RadioButton常用的技巧通過RadioGroup實現Tab,需要設置android:button="@null"。如果要在代碼中動態創建控件,android中並不能找到相關的屬性或方法。搜索均無解決辦法,最後想到一個變通的方法:通過透明色獲取drawable。
setButtonDrawable(getResources().getDrawable(android.R.color.transparent))
實際還是可以通過佈局的方法來動態創建控件。先創建一個RadioButton的rb.xml
<?xml version="1.0" encoding="utf-8"?> <RadioButton xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@null" > </RadioButton>
再在代碼生成RadioButton
RadioButton rb =(RadioButton)LayoutInflater.from(getContext()).inflate(R.layout.rb, null);
這種方式的好處是樣式等屬性可以在佈局中統一指定省的查sdk尋找相關屬性的設置方法。