【零基础学android】之Android TextView(六)

一、应用场景:设置文字字体,颜色,大小等
二、效果图:
1,“确认”前:
在这里插入图片描述

2,“确认”后:
在这里插入图片描述
三、代码:
MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button)this.findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO Auto-generated method stub
                setTitle("button1 被您点击");
                Log.i("widgetDemo", "button1 被您点击。");
                //定义textView为属性为TextView,代表id为textView2的控件
                TextView textView = (TextView)findViewById(R.id.textView2);
                //设置内容,由于此项在@string里已经设置过,故可要可不要
                textView.setText("设置文本名字颜色大小字体");
                //设置颜色
                textView.setTextColor(Color.RED);
                //设置大小
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
                //设置字体
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
        }); 
    }

.xml中的配置

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="69dp"
        android:text="@string/textview2" />
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="31dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button1" />

string.xml:

<string name="app_name">TextViewApp</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="button1">确认</string>
    <string name="textview2">设置文本</string>
    

四、技术总结

通过 setText() 更改 textView 的显示内容为“设置文本名字颜色大小字体”。
通过 setTextColor() 修改 textView 显示字体颜色为红色。
通过 setTextSize() 修改 textView 显示字体大小为 20sp。
通过 setTypeface() 修改 textView 显示字体风格为加粗。

也可以在.xml中设置上述内容
代码如下:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置文本名字颜色大小字体"
    android:textColor="#ff0000"
    android:textSize="20sp"
    android:textStyle="bold"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `SpannableString` 实现在 `TextView` 中嵌套另一个 `TextView` 的效果。 示例代码如下: ```java TextView textView = findViewById(R.id.text_view); SpannableString spannableString = new SpannableString("这是一个TextView,里面嵌套着另一个TextView"); TextView nestedTextView = new TextView(this); nestedTextView.setText("嵌套的TextView"); nestedTextView.setTextSize(18); nestedTextView.setTextColor(Color.BLUE); spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new ForegroundColorSpan(Color.GREEN), 8, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new ForegroundColorSpan(Color.YELLOW), 15, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new CustomSpan(nestedTextView), 15, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableString); ``` 其中,`CustomSpan` 是自定义的 `Span`,用于将 `TextView` 嵌套进去。示例代码如下: ```java public class CustomSpan extends ReplacementSpan { private final TextView textView; public CustomSpan(TextView textView) { this.textView = textView; } @Override public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) { return (int) (textView.getPaint().measureText(text, start, end) + 0.5f); } @Override public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) { textView.layout(0, 0, textView.getWidth(), textView.getHeight()); textView.draw(canvas); } } ``` 注意,因为嵌套的 `TextView` 是动态创建的,所以需要在 `draw` 方法中先对其进行布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值