换行
TextView中可以通过“\n”换行
行间距
Android中,在TextView显示中文时字间会比较紧凑,不是很美观。
为了让每行保持一定的行间距,可以设置属性android:lineSpacingExtra或android:lineSpacingMultiplier。
- android:lineSpacingExtra 设置行间距
android:lineSpacingExtra="6dp"
设置行间距为”6dp″。
- 设置行间距的倍数 android:lineSpacingMultiplier
android:lineSpacingMultiplier="2"
设置行间距倍数为 2″。
字体颜色
- 用于静态或初始文字颜色
main.xml中:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"
android:textColor="@color/red"
/>
在color.xml中设置颜色
<resources>
<color name="red">#FF0000</color>
</resources>
2. 在Activity类中设置
main.xml中,没有android:textColor=”@color/red”
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"
/>
在mainActivity中:
获得文本控件TextView,通过TextView的setTextColor方法进行文本颜色的设置,有3种方法:
- 使用系统自带的颜色类
tv.setTextColor(Color.RED);
使用颜色整数
tv.setTextColor(0xfff5f5f5);
0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,f5f5f5表示颜色。
注意:必须是8位的颜色整数,不接受f5f5f5这种6位的颜色表示。通过获得资源文件进行设置
tv.setTextColor(this.getResources().getColor(R.color.red));
根据不同的情况,R.color.red也可以是R.string.red或者R.drawable.red。
对应的需要在相应的配置文件里做相应的配置:
<color name="red">#FF0000</color>
<drawable name="red">#FF0000</drawable>
<string name="red">#FF0000</string>
代码:
public class mainActivity extends Activity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)this.findViewById(R.id.tv01);
tv.setTextColor(Color.RED);
// tv.setTextColor(0xff000000);
// tv.setTextColor(Color.rgb(205, 92, 92));
}
}
- 3.