在面试的过程中有些面试官问到TextView设置图片的问题。这是TextView自带的属性,没有什么高深的。现在我把之前用到过的一写我们不大常用的属性给大家介绍一下。
1.TextView 设置图片,并指定图片和文字的边距。
<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/ic_launcher"
android:gravity="center"
android:drawablePadding="10dp"/>
其中android:drawableLeft="@mipmap/ic_launcher",的意思是将图片设置在文字的左边,对应还有
在底部android:drawableBottom="@mipmap/ic_launcher"
在右边android:drawableRight="@mipmap/ic_launcher"
在顶部android:drawableTop="@mipmap/ic_launcher"
设置图片与TextView边界的距离
android:drawablePadding="10dp"
2.TextView 上下滚动。
在我们处理TextView显示文字的时候有时候内显示的内容太多,内容区域不能将内容全部显示出来,大多数我们会想到把我们的TextView嵌套到Srollview 中,通过实现上下滚动显示文本内容。其实TextView自身就有上下滚动的属性。
<TextView
android:id="@+id/textview"
android:text="@string/str_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:maxLines="4"
android:background="#FFF"
android:textColor="#000"
android:textSize="20sp"
android:layout_margin="10dp"
android:scrollbars="vertical"
android:isScrollContainer="true"
android:scrollbarStyle="outsideOverlay"
android:padding="10dp"
android:scrollbarFadeDuration="2000"
/>
在java代码中还要添加
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
要不然还是没法滚动
3.TextView 设置不同的颜色
通过SpannableString 设置不同的字体颜色
String str="终于等到你,还好我没放弃!";
SpannableString spannableString= new SpannableString(str);
spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(Color.GREEN), 6, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
4.TextView 左右滚动,设置左右自动滚动效果。
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="5.0dip"
android:gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:text="终于等到你,还好我没放弃,幸福来得好不容易,才会让人更加更加珍惜,在最好的年纪等到你,才算没有辜负自己"
android:textColor="#FF8FF52F"
android:textSize="20.0sp" />
希望能给需要的朋友一些帮助,同是希望得更多大牛们的知道和指正。