目录
View控件
通用属性
处理文本内容的View(TextView)
引用较长文本时:res--values--的strings_xml文件中用<string name="long_text">所引用的文本</string>
运行时需要修改,蓝色区段时最先运行的部分
android:textSize 设置字体大小 android:textColor 设置字体颜色 android:lineSpacingMultiplier="2" 调整几倍行距 ScrollView 滚动条 在滚动条里只能放一个直接子控件
android:singleLine="true" 取消换行操作(已过时) 替换:android:lines="1" 设置行数,单行 android:ellipsize="start" 设置省略号(...)的位置(start 开头, middle 中间,end 结尾,marquee 跑马灯) android:ellipsize="marquee" 跑马灯 需搭配使用 android:focusable="true" 设置可以获取焦点 android:focusableInTouchMode="true" 设置在触摸时可以获取焦点 android:marqueeRepeatLimit="marquee_forever" 设置跑马灯的时长(重复次数) 注意需要设置 TextView text = findViewById(R.id.tfboys); text.setSelected(true);
.xml文件和Activity.java 文件
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".TextActivity">
<!--
android:singleLine="true" 取消换行操作(已过时)
替换:android:lines="1" 设置行数,单行
android:ellipsize="start" 设置省略号(...)的位置(start 开头,
middle 中间,end 结尾,marquee 跑马灯)
android:ellipsize="marquee" 跑马灯
需搭配使用
android:focusable="true" 设置可以获取焦点
android:focusableInTouchMode="true" 设置在触摸时可以获取焦点
android:marqueeRepeatLimit="marquee_forever" 设置跑马灯的时长(重复次数)
注意需要设置
TextView text = findViewById(R.id.tfboys);
text.setSelected(true);
-->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/long_text"
android:id="@+id/tfboys"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class TextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text2);
TextView text = findViewById(R.id.tfboys);
text.setSelected(true);
}
}
被点击的View(Button)
Button按钮的背景色调整
android:background="@android:color/transparent" 背景色设置为透明
解决办法
找到项目中的这两个xml文件 将 parent="Theme.MaterialComponents.DayNight.DarkActionBar" 修改为 parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" 即可解决
注册点击事件的方法
(1)自定义内部类
1.通过id获取按钮 2.为按钮注册点击事件监听器
(2)匿名内部类
匿名内部类适用于只有唯一操作的按钮
(3)当前Activity去实现事件接口
(4)在布局文件中添加点击事件属性
参数:被点击的控件对象
public void myClick(View v){
switch(v.getId()){
case R.id.but4:
Log.e("TAG","but4");
break;
case R.id.but5:
Log.e("TAG","but4");
break;
}
}
处理图片内容的View(ImageView)
用来显示和控制图像的控件,可以对它进行放大,缩小,旋转等操作;命名规则:英文字母(小写)、数字(不能用于开头)、下划线
常用属性有:
android:src=“ ” 指定前景图片资源
android:background=“ ” 设置背景
ImageButton 按钮,可以显示图片
接收用户信息输入的View(EditText)
属性设置
android:inputType 控制输入类型
textPassword 密码
number 只能是正整数
numberSigned 整数 numberDecimal 小数
android:inputType="numberSigned|numberDecimal" 各种类型数字都可以接收
phone 可以有数字和-
android:hint 提示性的文字,输入时提示消失
android:maxLength 限制输入长度
进度条类的View(ProgressBar)
进度条,默认情况下是圆形,没有刻度,只是一个不断旋转的动画效果。通过设置style,可以显示传统的水平带刻度进度条
style=“?android:attr/progressBarStyleHorizontal” (水平进度条风格)
android:progress=" " 设置进度
android:max=" " 设置最大值,默认为100
属性:android:indeterminate="true" 设置进度条是否一直滚动,"true"--是
在Android中,4.0以后是不能直接在线程中操作控件的,进度条是个特例