安卓_TextView(文本框)设置

TextView(文本框)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center"
    android:background="#8fffad">

    <TextView
        android:id="@+id/txtOne"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="center"
        android:text="TextView(显示框)"
        android:textColor="#EA5246"
        android:textStyle="bold|italic"
        android:background="#000000"
        android:textSize="18sp" />

</RelativeLayout>

id:TextView组件的id,Java代码—通过findViewById()—获取该对象—相关属性的设置
layout_width:组件宽度,wrap_content—内容多大,控件就多大
*match_parent(fill_parent)**,填满控件所在父容器;设置成特定的大小–例如显示效果,设置成了200dp。
layout_height:组件高度
gravity:控件内容对齐方向
text:设置显示文本,字符串写到string.xml文件中,通过@String/xxx取得字符串内容
textColor:字体颜色,可以通过colors.xml资源来引用
textStyle:字体风格,normal(无效果),bold(加粗),italic(斜体)
textSize:字体大小,单位sp!
background:控件背景颜色,填充整个控件颜色,可以是图
https://www.runoob.com/w3cnote/android-tutorial-textview.html

在这里插入图片描述

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/txtZi"
        android:drawableTop="@drawable/s_jump"
        android:drawableLeft="@drawable/s_jump"
        android:drawableRight="@drawable/s_jump"
        android:drawableBottom="@drawable/s_jump"
        android:drawablePadding="10dp"
        android:text="guanjian" />

</RelativeLayout>

package edu.ydk.myapplication;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView txtZi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtZi = (TextView) findViewById(R.id.txtZi);
        Drawable[] drawable = txtZi.getCompoundDrawables();
        // 数组下表0~3,依次是:左上右下
        drawable[1].setBounds(100, 0, 200, 200);
        txtZi.setCompoundDrawables(drawable[0], drawable[1], drawable[2],
                drawable[3]);
    }
}

反射法
在这里插入图片描述

package edu.ydk.myapplication;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.widget.TextView;

import java.lang.reflect.Field;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView t1 = (TextView) findViewById(R.id.txtZi);
//        String s1 = "图片:<img src = 'icon'/><br>";
        String s1 = "图片:<img src = 's_jump'/><br>";
        s1 += "<a href = 'http://www.baidu.com'>百度</a>";
//        t1.setText(Html.fromHtml(s1));

        t1.setText(Html.fromHtml(s1, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                Drawable draw = null;
                try {
                    // 根据资源ID变量名获得Field对象,用反射机制实现
                    Field field = R.drawable.class.getField(source);
                    // 取得并返回资源id字段(静态变量)值,使用反射机制
                    int resourceId = Integer.parseInt(field.get(null).toString());
                    draw = getResources().getDrawable(resourceId);
                    draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return draw;
            }
        }, null));
        t1.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值