• 安卓学习(6)


    今天学习了TextView的几个开发实例

    1 带阴影的TextView

    涉及到的几个属性:

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

    效果图:

    实现代码:

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

    2 带边框的TextView:

    实现效果图:

    代码实现:

    Step 1:编写矩形边框的Drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <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> 

    Step 2:编写圆角矩形边框的Drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- 设置透明背景色 -->
        <solid android:color="#87CEEB" />
    
        <!-- 设置一个黑色边框 -->
        <stroke
            android:width="2px"
            android:color="#000000" />
        <!-- 设置四个圆角的半径 -->
        <corners
            android:bottomLeftRadius="10px"
            android:bottomRightRadius="10px"
            android:topLeftRadius="10px"
            android:topRightRadius="10px" />
        <!-- 设置一下边距,让空间大一点 -->
        <padding
            android:bottom="5dp"
            android:left="5dp"
            android:right="5dp"
            android:top="5dp" />
            
    </shape>

    Step 3:将TextView的blackground属性设置成上面这两个Drawable:

    <LinearLayout 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"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/txtOne"
            android:layout_width="200dp"
            android:layout_height="64dp"
            android:textSize="18sp"
            android:gravity="center"
            android:background="@drawable/txt_rectborder"
            android:text="矩形边框的TextView" />
    
        <TextView
            android:id="@+id/txtTwo"
            android:layout_width="200dp"
            android:layout_height="64dp"
            android:layout_marginTop="10dp"
            android:textSize="18sp"
            android:gravity="center"
            android:background="@drawable/txt_radiuborder"
            android:text="圆角边框的TextView" />
    
    
    </LinearLayout>

    3 带图片(drawableXxx)的TextView:

    在实际开发中,我们可能会遇到这种需求:

    如图,要实现这种效果,可能你的想法是:一个ImageView用于显示图片 + 一个TextView用于显示文字,然后把他们丢到一个LinearLayout中,接着依次创建四个这样的小布局,再另外放到一个大的LinearLayout中,效果是可以实现,但是会不会有点繁琐呢?而且前面我们前面也说过,布局层次越少,性能越好!使用drawableXxx就可以省掉上面的过程,直接设置四个TextView就可以完成我们的需求!

    基本用法:

    设置图片的核心其实就是:drawableXxx;可以设置四个方向的图片: 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代码中来进行一个修改!

    示例代码如下:

    package com.jay.example.test;  
      
    import android.app.Activity;  
    import android.graphics.drawable.Drawable;  
    import android.os.Bundle;  
    import android.widget.TextView;  
      
    public class MainActivity extends Activity {  
        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]);  
        }  
    } 

    运行效果图:

    代码分析:

    • ①Drawable[] drawable = txtZQD.getCompoundDrawables( ); 获得四个不同方向上的图片资源,数组元素依次是:左上右下的图片
    • ②drawable[1].setBounds(100, 0, 200, 200); 接着获得资源后,可以调用setBounds设置左上右下坐标点,比如这里设置了代表的是: 长是:从离文字最左边开始100dp处到200dp处 宽是:从文字上方0dp处往上延伸200dp!
    • ③txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2], drawable[3]);为TextView重新设置drawable数组!没有图片可以用null代替哦! PS:另外,从上面看出我们也可以直接在Java代码中调用setCompoundDrawables为 TextView设置图片!
  • 相关阅读:
    手机端局部滚动问题 overflow-y:auto|scroll无效,使用iscroll解决
    h5 js 图片预览并判断 ajax上传
    安装一些包管理的记录 win10
    nginx php rewrite配置
    记一次u盘 无法格式化、0字节、写保护的解决过程
    erlang,elixir安装
    Redis详解:lists数据类型及操作
    redis类型[string 、list 、 set 、sorted set 、hash]
    java 生成Word文档
    mysql 获取当前月最后一天和第一天
  • 原文地址:https://www.cnblogs.com/shenaoyu/p/13063258.html
Copyright © 2020-2023  润新知