TextView

作者: 大灰狼zz | 来源:发表于2018-08-29 11:43 被阅读0次

基础属性

<TextView
            android:gravity="right"                   设置文本显示位置(居右)
            android:padding="5dp"                     设置内部间距
            android:text="忘记密码?"                  设置文本
            android:textColor="@color/basic_gray1"    设置字体颜色
            android:textSize="12sp"                   设置字体大小
            android:textStyle=""                      设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体)
/>

设置字间距和行间距

字间距:

android:textScaleX:控制字体水平方向的缩放,默认值1.0f,值是float
Java中setScaleX(2.0f); 

行间距: Android系统中TextView默认显示中文时会比较紧凑,为了让每行保持行间距

android:lineSpacingExtra:设置行间距,如"3dp" 

android:lineSpacingMultiplier:设置行间距的倍数,如"1.2"

Java代码中可以通过: setLineSpacing方法来设置

自动换行

自动换行通过 android:singleLine 设置,默认为 false。

如需要自动换行,可以用:

android:singleLine = "false"

如果要在一行显示完,不换行,可以用:

android:singleLine = "true"

除此之外,可以也设置多行显示不完,添加个maxLines的属性即可

android:maxLines = "3"

省略号和跑马灯形式

TextView及其子类,当字符内容太长显示不下时可以省略号代替未显示的字符;省略号可以在显示区域的起始,中间,结束位置,或者以跑马灯的方式显示文字(textview的状态为被选中)。 注意:要和android:maxLines="1"一起用

        android:ellipsize="start"        省略号在开头        
        android:ellipsize="middle"       省略号在中间        
        android:ellipsize="end"          省略号在结尾        
        android:ellipsize="marquee"      跑马灯显示

       或者在程序中可通过setEillpsize显式设置。
       注: EditText不支持marquee这种模式。

带图片(drawableXxx)的TextView

img

设置图片的核心其实就是:drawableXxx;可以设置四个方向的图片:
drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右) 另外,你也可以使用drawablePadding来设置图片与文字间的间距!

<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="哈哈" />  

代码设置图片
setCompoundDrawables与setCompoundDrawablesWithIntrinsicBounds的区别
setCompoundDrawablesWithIntrinsicBounds(设置原图)
setCompoundDrawables(可以调整图片大小)
例1:

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[2].setBounds(0, 0, 40, 40);  
        txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],  
                drawable[3]);  
    }  
} 

例2:

        mNextButton=(Button)findViewById(R.id.next_button);
        Drawable drawable=getResources().getDrawable(R.drawable.im14);
        drawable.setBounds(0,0,width,height);//必须设置,否则无效
        mNextButton.setCompoundDrawables(null,null,drawable,null);
        //width即为你需要设置的图片宽度,height即为你设置的图片的高度

例3:
根据系统语言设置图标位置

//根据系统语言设置图标的显示位置
        String language = Locale.getDefault().getLanguage();
        if (language.equals("ar") || language.equals("fa")) {
            //如果是从右到左阅读的语言(阿拉伯等)
            Drawable drawable = getResources().getDrawable(R.drawable.edit);
            drawable.setBounds(0, 0, drawable.getMinimumWidth()/2, drawable.getMinimumHeight()/2);
            tv_sign.setCompoundDrawables(drawable, null, null, null);//设置textView四个方向上的图片
            tv_sign.setCompoundDrawablePadding(5);//间距
        } else {
            //从左到右
            Drawable drawable = getResources().getDrawable(R.drawable.edit);
            drawable.setBounds(0, 0, drawable.getMinimumWidth()/2, drawable.getMinimumHeight()/2);
            tv_sign.setCompoundDrawables(null, null, drawable, null);
            tv_sign.setCompoundDrawablePadding(5);
        }

实现跑马灯效果的TextView

img

代码实现:

    <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="你整天说着日了狗日了狗,但是你却没有来,呵呵呵呵呵呵呵呵呵呵~"/>

各种Span设置

Android 一个TextView中设置文字不同字体大小和颜色的最完整方法
将TextView的字体设置为大小不一
android 一个 textview 设置不同的字体大小和颜色
步骤如下:

  1.定义不同style .

不妨如下定义2个style

    <style name="style0">
        <item name="android:textSize">19dip</item>
        <item name="android:textColor">@color/color1</item>
    </style>
    
    <style name="style1">
        <item name="android:textSize">23dip</item>
        <item name="android:textColor">@color/color2</item>
        <item name="android:textStyle">italic</item>
    </style>

2 . 通过SpannableString 设置字符串格式。代码如下:

mTextView = (TextView)findViewById(R.id.test);
         SpannableString styledText = new SpannableString("亲爱的小宝,你好");
         styledText.setSpan(new TextAppearanceSpan(this, R.style.style0), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
         styledText.setSpan(new TextAppearanceSpan(this, R.style.style1), 3, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        mTextView.setText(styledText, TextView.BufferType.SPANNABLE);

使用autoLink属性识别链接类型

img

为TextView设置两种状态,程序中可以动态切换

为TextView设置两种状态,程序中可以动态切换

经常会需要用文字的两种状态来表示当前系统的某两种状态。比如:

image

这里的第一个TextView和后两个TextView就表示了两种状态。我们可以在程序的动态的切换状态(而不是直接修改颜色)

可以利用TextView的enable属性实现:

在res中建立一个color文件夹,在其中新建一个xml(xxx.xml):

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

<item android:state_enable=*"false" android:color*=*"@color/white"></item>*

<item android:color=*"@color/login_footerbutton_n"></item>*

</selector>

TextView的属性加一条:

android:textColor=*"@color/xxx"*

TextView的enable的属性默认为true。

在程序中设置TextView的状态:

tabTextView.setEnabled(**false**);

换行后居中显示

android自定义换行居中CenterTextView
TextView换行居中,每行居中显示

常见问题

相关文章

网友评论

      本文标题:TextView

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