安卓_TextView(文本框)设置

  • Post author:
  • Post category:其他


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());
    }
}



版权声明:本文为ydk001001原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。