美文网首页
安卓学习笔记-----我所不熟悉的TexetView的一些属性

安卓学习笔记-----我所不熟悉的TexetView的一些属性

作者: 天青色等烟雨hhft | 来源:发表于2017-12-03 11:41 被阅读0次
带阴影的TextView

涉及到的几个属性:
android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用
android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置


image.png

实现代码

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:shadowColor="#F9F900"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowRadius="3.0"
        android:text="带阴影的TextView"
        android:textColor="#4A4AFF"
        android:textSize="30sp" />


带边框的TextView
image.png

1.编写shape

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 设置一个黑色边框 -->
    <stroke android:width="2px" android:color="#000000"/>
    <!-- 渐变 -->
    <gradient
        android:angle="270"
        android:endColor="#C0C0C0"
        android:startColor="#FCD209" />
    <!-- 设置边距 -->
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>

</shape>

2.在布局文件中作为background使用

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:shadowColor="#F9F900"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowRadius="3.0"
        android:text="带阴影的TextView"
        android:textColor="#4A4AFF"
        android:background="@drawable/shape"
        android:textSize="30sp" />
带图片的TextView
image.png

实现代码

<TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="lxx"
        android:layout_centerInParent="true"
        android:textColor="#4A4AFF"
        android:drawablePadding="10dp"
        android:drawableRight="@drawable/ic_launcher_background"
        android:drawableTop="@drawable/ic_launcher_background"
        android:drawableLeft="@drawable/ic_launcher_background"
        android:drawableBottom="@drawable/ic_launcher_background"
        android:textSize="30sp" />

之前用这个属性的时候就很疑惑,因为他不能设置大小,今天才知道原来是可以在java代码中设置的,效果:


image.png

代码

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

drawable[1].setBounds(100, 0, 200, 200);调用setBounds设置左上右下坐标点,比如这里设置了代表的是: 长是:从离文字最左边开始100dp处到200dp处 宽是:从文字上方0dp处往上延伸200dp!

使用autoLink属性识别链接类型
image.png
实现跑马灯效果的TextView

实现代码

<TextView
        android:id="@+id/txtOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="今天周末啦啦啦啦啦啦啦啦啦~~~~"/>
自动换行

自动换行通过 android:singleLine 设置,默认为 false。
自动换行用:android:singleLine = "false"
如果要在一行显示完,不换行,用:android:singleLine = "true"

相关文章

网友评论

      本文标题:安卓学习笔记-----我所不熟悉的TexetView的一些属性

      本文链接:https://www.haomeiwen.com/subject/zhlabxtx.html