图片中的解释如下
<TextView
android:layout_width="wrap_content" //组件宽度
android:layout_height="wrap_content" //组件高度 单位一般用dp
android:id="@+id/tv_one" //为TextView去一个名字
android:text="享学" //设置组件的文本
我们一般不会直接给“享学”赋给text
而是在 res/string.xml 中给成这样,从此处以下第3行。
1 <resources>
2 <string name="app_name">kzjTextView1</string>
3 <string name="tv_one">享学</string>
4 </resources>
我们通过以下方式获得该值(引用的方式)
android:text="@string/tv_one"
android:textColor="#ff000000" //设置组件的颜色,8位,前两位代表透明度
颜色的设置也一样,在values/colors.xml 中添加颜色
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="red">#FFFF0000</color>
</resources>
通过调用的方式
android:background="@color/red"
android:textStyle="italic" //设置组件中的字体风格,normal(无效果),bold(j加粗),italic(x斜体)
android:textSize="30sp" //设置组件的字体大小,单位一般用sp
android:background="#FFFF0000" //设置背景颜色,填充整个控件,也可以是图片
背景颜色同字体颜色一样,也可以用如上方法
可以设置字体的阴影,阴影要与背景的颜色不同,否则无效果
android:shadowColor="@color/green" //设置阴影的颜色
android:shadowDx="10.0" //设置阴影的横向偏移量
android:shadowDy="10.0" //设置阴影的纵向偏移量
android:shadowRadius="3.0" //设置阴影的强度,从 0.1到3.0
android:gravity="center" //设置空件的中的内容格式,center为居中,在TextView中是文字格式
在ImageView中是图片格式
为字体设置一个滚动效果
1 android:singleLine="true" //内容单行显示
2 android:ellipsize="marquee" //在哪里省略文本
3 android:marqueeRepeatLimit="marquee_forever" //字幕动画重复的次数
4 android:focusable="true" //是否可以获取焦点
5 android:focusableInTouchMode="true" //用于控制视图在触摸模式下是否可以聚焦
加第6行语句是第一种解决办法
6 android:clickable="true"
第二种解决办法
滚动效果的解决办法还有在java文件中在创建一个MyTextView文件,继承TextView,重写前三种方法,加isFocused()方法
如下
// package com.example.kzjtextview1;
// import android.content.Context;
// import android.util.AttributeSet;
// import android.widget.TextView;
// import androidx.annotation.Nullable;
// public class MyTextView extends TextView {
// public MyTextView(Context context) {
// super(context);
// }
// public MyTextView(Context context, @Nullable AttributeSet attrs) {
// super(context, attrs);
// }
// public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
// super(context, attrs, defStyleAttr);
// }
// @Override
// public boolean isFocused() {
// return true;
// }
// }
如此写法layout包下的xml文件的<TextView 开头句要给为<com.example.kzjtextview1.MyTextView
第三种方法
在第一种方法下去除最后一个语句,改为如下这样
<TextView
..................... 中间的内容不变,/> 改为> ,>后写<requestFocus/>语句
>
<requestFocus/>
</TextView>
/>