为什么这段代码无法让textview 文字居中居右

在带(继承)TextView的控件中,在代码中动态更改TextView的文字颜色
今天因为公司项目需求,需要实现一种类似tab的选项卡,当时直接想到的就是使用RadioGroup和RadioButton来实现。这个方法完全没问题。但是在后来的开发过程中,却遇到了一些困扰很久的小困难。大概需求是:在代码中,动态的获取tab的个数,然后初始化RadioGroup,每一个tab对应一个RadioButton,即加入一个tab就要向RadioGroup中add一个RadioButton,然后在按钮选中时要更改文字颜色。因为是动态添加,所以无法在xml中配置了RadioButton和设置selector来更改文字颜色了。
下面贴上部分代码:
int size = lvl1.size();
for (int i = 0; i < i++) {
Question q = lvl1.get(i);
RadioButton btn = getRadioButton(q, i);
radioGroup.addView(btn,
new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.MATCH_PARENT, 1));
if(i == 0){
subLevel.clear();
subLevel.addAll(q.getSubLevel());
adapter.notifyDataSetChanged();
// 数据变化了
btn.setChecked(true);
先看看上面这段代码。我通过getRadioButton()来动态生成一个RadioButton,然后调用addView()添加到RadioGroup中,此时,需要注意的是:
radioGroup.addView(btn,
new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.MATCH_PARENT, 1));方法中指定了RadioButton的布局参数,要注意,这里一定要使用RadioGroup.LayoutParams这个类来指定RadioButton的布局参数,如果你使用其他的例如:
LinearLayout.LayoutParams、ViewGroup.LayoutParams等类,将不会起作用!!
这就是我第一个遇到的小坑。为什么会导致这样呢?因为在RadioGroup中,它已经重写了LayoutParams了,当时我大概看了一下,估计就是在这里面有变动造成的。
再来看看,getRadioButton这个方法:
private RadioButton getRadioButton(Question q, int index)
RadioButton btn = new RadioButton(context);
btn.setId(index);
Drawable d = context.getResources().getDrawable(R.drawable.radiogroup_tab_selector);
d.setBounds(0, 25, 150, 55);
btn.setButtonDrawable(R.drawable.transparent);
btn.setCompoundDrawables(null, null, null, d);
btn.setText(q.getQs_content());
btn.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
/* 在代码中使用selector来改变选中按钮文字颜色,需要使用getColorStateList(int id),这个方法来解析我们定义selector,
使用getColor()是解析不出来的。
btn.setTextColor(context.getResources().getColorStateList(R.color.acs_tab_textcolor_selector));
}上面代码中有两个地方要注意:
1. 在调用setButtonDrawables()时,需要先调用了Drawable的setBounds()方法,这样设置的Drawable才能够显示出来
2. 在代码中,使用selector来动态改变RadioButton选中时文字颜色,需要使用getColorStateList(int id),这个方法来解析我们定义selector,使用getColor()是解析不出来的。
总结:虽然上面我是以RadioButton作为例子,但是,只要是继承TextView的控件,需要在代码中动态改变文本颜色,都需要注意上面提到的地方。
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。当前位置: >
> 代码生成的TextView怎样设置样式,该如何处理
代码生成的TextView怎样设置样式,该如何处理
qvbpesrg & at
代码生成的TextView怎样设置样式TextView text= new TextView(this);如何把text设置成以下样式?&TextView &
android:layout_width=&wrap_content& &
android:layout_height=&22dp& &
android:layout_weight=&9& &
android:textSize=&13dp& &
android:gravity=&center_vertical& &
android:layout_marginLeft=&10dp& &
android:textColor=&#000000& /&谢谢。
TextView text= new TextView(this);text.setWidth(&22dip&);text.setHeight(&10dip&);后面的楼主自己写吧,有问题接着问!
qw032011 & &
& & (0)(0)应该是:TextView text = new TextView(this);text.setLayoutParams(new LayoutParams(..........这里写LayoutParams常量));xml中的属性大部分可以用代码设置,但建议楼主最好写在xml文件中,避免代码过于混乱!当有很多控件要实现的话,你用代码实现会显得太麻烦,也不友好!别人读你的代码会很困难!但是,有一个好处就是可以动态改变控件的样式!qw1212 & &
& & (0)(0)
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&&&湘教QS2-164&&增值电信业务经营许可证湘B2-使用代码为textview设置drawableLeft
原问题描述:
xml中的textView:
&TextView &
& & & & android:id=&@+id/bookTitle& &
& & & & android:layout_width=&match_parent& &
& & & & android:layout_height=&wrap_content& &
& & & & android:layout_weight=&1& &
& & & & android:drawableLeft=&@drawable/checkmark& &
& & & & android:gravity=&center_vertical& &
& & & & android:textStyle=&bold& &
& & & & android:textSize=&24dip& &
& & & & android:maxLines=&1& &
& & & & android:ellipsize=&end&/& &
如程序中所见我在xml中设置了 DrawableLeft。
我想在代码中改变drawable。
有什么方法可以使用代码为textview设置drawableLeft呢?
解决方案:
public void &setCompoundDrawables &(Drawable left, Drawable top, Drawable right, Drawable bottom); &
类似调用方法如下:
1.在XML中使用
android:drawableLeft=&@drawable/icon& &
2.代码中动态变化
Drawable drawable= getResources().getDrawable(R.drawable.drawable); &
/// 这一步必须要做,否则不会显示. &
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); &
myTextview.setCompoundDrawables(drawable,null,null,null); &
也或参考另一个函数
public void setCompoundDrawablesWithIntrinsicBounds (Drawable left, &
Drawable top, Drawable right, Drawable bottom) &
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。1.6的sdk下
android:id="@+id/Button"
android:layout_width="300px"
android:layout_height="250px"
android:text="@string/app_name"
android:background="@drawable/icon"
这段代码 添加了
android:gravity="center_horizontal"
上面的字就不显示是为什么?
添加前:
这个问题可以反复再现绝不是rp问题.
问题补充:原因不是那个~,是设置水平居中后文字没有在空间内不垂直居中跑到top的位置,由于图片边缘透明,字体呈黑色,背景是黑色,所以出现看不见的现象。
问题补充:liveHappy 写道android:layout_width="300px"
android:layout_height="250px"
正常你这么设置时没有起作用的,你可以自己试验一下,你单设置height=250,width是一个样,然后你把width改成300,你会发现根本没变,还是那样,你要仔细会发现width既然是250 ,显示的是个正方形。正因为这样,实际你设置的和显示的有区别,当你在加上android:gravity="center_horizontal" 的时候水平上看不到了。
所以按照正常来讲下面的代码是可以的,绝对没问题。
&TextView
android:text="red"
android:gravity="center_horizontal" //这里字水平居中
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
原因不是那个~,是设置水平居中后文字没有在空间内不垂直居中跑到top的位置,由于图片边缘透明,字体呈黑色,背景是黑色,所以出现看不见的现象。
android:layout_width="300px"
android:layout_height="250px"
正常你这么设置时没有起作用的,你可以自己试验一下,你单设置height=250,width是一个样,然后你把width改成300,你会发现根本没变,还是那样,你要仔细会发现width既然是250 ,显示的是个正方形。正因为这样,实际你设置的和显示的有区别,当你在加上android:gravity="center_horizontal" 的时候水平上看不到了。
所以按照正常来讲下面的代码是可以的,绝对没问题。
&TextView
android:text="red"
android:gravity="center_horizontal" //这里字水平居中
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
已解决问题
未解决问题

我要回帖

更多关于 textview 文字居中 的文章

 

随机推荐