怎么在代码中动态改变radioButton的文字位置
如何在代码中动态改变radioButton的文字位置?
我在代码中为radioGroup生成了若干个radioButton,如下图所示:

现在我希望将显示方式改为如下图所示,应该在代码中怎么修改?

注意:我不是在XML中定义的radioGroup和radioButton,而是动态添加的
------解决方案--------------------
android:drawableBottom setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) The drawable to be drawn below the text.
把你的数字换成图片 用这个属性试试看
------解决方案--------------------
自己做一个带选中状态的drawable,然后把这个drawable设置成radio button的drawableTop,再去掉系统自带的button
------解决方案--------------------
刚才大概看了一下RadioButton的代码,好像是通过setButtonDrawable不能将button设置成null
试试通过xml把button设置成null
如果你需要通过代码生成一个RadioButton的话,可以这样
1. 定义XML
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:button="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
2. 创建RadioButton
RadioButton newButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.radio_without_button, parent, false);
------解决方案--------------------
lz你要的是不是类似这样动态添加的代码?
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
String[] titles = new String[7];
titles[0] = "热门";
titles[1] = "投资";
titles[2] = "融资";
titles[3] = "合作";
titles[4] = "新品";
titles[5] = "招聘";
titles[6] = "求职";
for(int i=0; i<titles.length; i++)
{
RadioBtnEx btn = new RadioBtnEx(this);
btn.setTextSize(20);
RadioGroup.LayoutParams btnParam = new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnParam.leftMargin = 10;
btnParam.topMargin = 2;
btn.setText(titles[i]);
rg.addView(btn, btnParam);
}
我在代码中为radioGroup生成了若干个radioButton,如下图所示:
现在我希望将显示方式改为如下图所示,应该在代码中怎么修改?
注意:我不是在XML中定义的radioGroup和radioButton,而是动态添加的
------解决方案--------------------
android:drawableBottom setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) The drawable to be drawn below the text.
把你的数字换成图片 用这个属性试试看
------解决方案--------------------
自己做一个带选中状态的drawable,然后把这个drawable设置成radio button的drawableTop,再去掉系统自带的button
------解决方案--------------------
刚才大概看了一下RadioButton的代码,好像是通过setButtonDrawable不能将button设置成null
试试通过xml把button设置成null
如果你需要通过代码生成一个RadioButton的话,可以这样
1. 定义XML
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:button="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
2. 创建RadioButton
RadioButton newButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.radio_without_button, parent, false);
------解决方案--------------------
lz你要的是不是类似这样动态添加的代码?
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
String[] titles = new String[7];
titles[0] = "热门";
titles[1] = "投资";
titles[2] = "融资";
titles[3] = "合作";
titles[4] = "新品";
titles[5] = "招聘";
titles[6] = "求职";
for(int i=0; i<titles.length; i++)
{
RadioBtnEx btn = new RadioBtnEx(this);
btn.setTextSize(20);
RadioGroup.LayoutParams btnParam = new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnParam.leftMargin = 10;
btnParam.topMargin = 2;
btn.setText(titles[i]);
rg.addView(btn, btnParam);
}