当前位置: 首页 > java > 正文

安卓常用控件之TextView

admin 发表于2022年9月14日 15:04

TextView

1、基础属性


<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>

2、实际开发

2.1 带阴影的textView





2.2 带边框的TextView

shapeDrawable,画



2.3 带图片(drawableXxx)的TextView:

这里就把图片直接放在Drawable文件夹下就行


然后在TextView中可以设置四个方向的图 片: drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右) 另外,也可以使用drawablePadding来设置图片与文字间的间距!

<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="com.jay.example.test.MainActivity" >  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerInParent="true"  
        android:drawableTop="@drawable/show1"  
        android:drawableLeft="@drawable/show1"  
        android:drawableRight="@drawable/show1"  
        android:drawableBottom="@drawable/show1"  
        android:drawablePadding="10dp"  
        android:text="张全蛋" />  
  
</RelativeLayout> 

这样设置的drawable并不能自行设置大小,在XML是无法直接设置的; 所以我们需要在Java代码中来进行一个修改!

public class MainActivity extends AppCompatActivity {

    private TextView txtZQD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtZQD = (TextView) findViewById(R.id.txtZQD);
        Drawable[] drawable = txtZQD.getCompoundDrawables();   //获得四个方向的图片资源放到数组
        // 数组下表0~3,依次是:左上右下
        drawable[1].setBounds(100, 0, 200, 200);
        txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],
                drawable[3]);
    }
}


2.4 使用autoLink属性识别链接类型

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://www.baidu.com"
android:autoLink="web"
android:textColor="#000000"
android:textSize="30sp"
android:background="@drawable/txt_rectborder"/>

2.5 TextView玩转HTML

2.6 SpannableString&SpannableStringBuilder定制文本

这里的用到再看吧https://www.runoob.com/w3cnote/android-tutorial-textview.html



后面还有啥跑马灯之类的,也是用到再说吧。


全文完
本文标签: android
本文标题: 安卓常用控件之TextView
本文链接: http://blog.qqzzz.net/m/?post=142

〓 随机文章推荐

共有644阅 / 0我要评论
  1. 还没有评论呢,快抢沙发~

发表你的评论吧返回顶部

!评论内容需包含中文

请勾选本项再提交评论