布局中通常会用到@null。如RadioButton常用的技巧通过RadioGroup实现Tab,需要设置android:button="@null"。如果要在代码中动态创建控件,android中并不能找到相关的属性或方法。搜索均无解决办法,最后想到一个变通的方法:通过透明色获取drawable。

Java代码
  1. setButtonDrawable(getResources().getDrawable(android.R.color.transparent))

实际还是可以通过布局的方法来动态创建控件。先创建一个RadioButton的rb.xml

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RadioButtonxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:button="@null">
  6. </RadioButton>

再在代码生成RadioButton

Java代码
  1. RadioButtonrb=(RadioButton)LayoutInflater.from(getContext()).inflate(R.layout.rb,null);

这种方式的好处是样式等属性可以在布局中统一指定省的查sdk寻找相关属性的设置方法。

更多相关文章

  1. Android系统java层次service实现
  2. Android中抛出android.app.Fragment$InstantiationException异常
  3. Android上实现一个简单的天气预报APP(二) 配置布局
  4. OkHttp学习系列二:谈谈Android中使用的坑
  5. Android(安卓)学习之路3
  6. 4 行代码实现 ANDROID 快速文件下载
  7. ART模式下dex2oat出错导致系统无法正常启动
  8. 简述Android六大布局
  9. Xamarin Android开发实战(上册)

随机推荐

  1. 【Android】沉浸式状态栏完美解决方案
  2. Android开发包下载(包括开发所需所有安装
  3. Android DrawerLayout 侧滑菜单
  4. Android 网络编程之---HttpClient 与 Htt
  5. Android(安卓)UI--动画 Animation
  6. Android(安卓)Layout Binder(在线将XML中V
  7. android中Activity与service之间相互通信
  8. Android之adb命令
  9. For Best User Experience & UI
  10. Android的AsyncTask异步任务浅析