本文最后更新于298 天前,其中的信息可能已经过时,如有错误请发送邮件到big_fw@foxmail.com
1.TextView
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/zhi" android:textSize="22sp" android:textColor="#00ffff" android:lineSpacingMultiplier="1" android:lineSpacingExtra="15sp"/>
重要属性
- android:textSize 设置字体大小
- android:textColor 设置字体颜色
- android:lineSpacingMultiplier 设置倍距
- android:lineSpacingExtra 设置行间距
如何实现文字自动滚动(跑马灯效果)
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/zhi" android:singleLine="true" android:lineSpacingMultiplier="1" android:ellipsize="middle" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever"/>
- android:singleLine 设置单行
- android:ellipsize 设置省略号的位置
- android:focusable 设置可以获取焦点
- android:focusableInTouchMode 设置在触摸时可以获取焦点
- android:marqueeRepeatLimit 设置跑马灯时长
2.EditText
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="20dp" android:inputType="text" android:hint="请输入用户名" android:textColorHint="#cccccc"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="20dp" android:inputType="textPassword" android:hint="请输入密码" android:textColorHint="#cccccc" android:maxLength="11"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="20dp" android:inputType="textEmailAddress" android:hint="请输入邮箱" android:textColorHint="#cccccc"/>
重要属性
- android:inputType 输入类型
- textPassword 密码
- number 只能正整数
- numberSigned 整数
- numberDecimal 小数
- android:hint 设置阴影文字
- android:hintColor 设置阴影文字颜色
3.Button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过自定义内部类实现点击事件"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过匿名内部类实现点击事件"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过当前activity实现点击事件"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="在xml文件中绑定"/> </LinearLayout>
1.自定义内部类实现点击事件
public class ButtonActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button); Button btn1 = findViewById(R.id.btn1); //点击事件:被点击时触发的事件 MyClickListener mcl = new MyClickListener(); btn1.setOnClickListener(mcl);//为按钮注册点击事件监听器 } class MyClickListener implements View.OnClickListener{ @Override public void onClick(View view) { Log.e("TAG","刚刚点击了按钮!"); } } }
2.匿名内部类实现点击事件
btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e("TAG","刚刚点击了按钮!"); } });
3.当前Activity实现点击事件
public class ButtonActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button); Button btn3 = findViewById(R.id.btn3); btn3.setOnClickListener(this); } @Override public void onClick(View view) { Log.e("TAG","刚刚点击了按钮!"); } }
3.XML中实现绑定点击事件
<Button android:id="@+id/btn4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="在xml文件中绑定" android:onClick="myClick"/>
在Java中实现myClick方法即可。
4.ImageView
用来显示和控制图像的控件,可以对它进行放大缩小旋转等操作。
重要属性
- android:src 设置前景图片资源
- android:background 设置背景
<ImageView android:layout_width="69dp" android:layout_height="75dp" android:src="@mipmap/user" android:layout_margin="10dp"/>
5.ProgressBar
进度条,默认情况下是圆形,没有刻度,只是一个不断旋转的动画效果。通过设置style,可以显示传统的水平带刻度进度条。
重要属性
- android:style 设置风格
- android:progress 设置进度
- android:max 设置进度最大值,默认100
- android:indeterminate 设置进度条不停滚动
<ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progress" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:progress="30" android:max="100"/> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:indeterminate="true"/>
public class ProgressBarActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress_bar); ProgressBar progressBar = findViewById(R.id.progress); progressBar.setProgress(80); //在android中,4.0以后是不能在线程中操作控件的 new Thread(){ public void run(){ for(int i=0;i<=100;i++){ progressBar.setProgress(i); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } }
6.CheckBox
系统封装的复选控件
两种状态:选中和未选中 setChecked() isChecked()
监听状态变化 setOnCheckedChangeListener()
<CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox" />
public class CheckBoxActivity extends AppCompatActivity { private static final String TAG = "CheckBoxActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_check_box); CheckBox checkBox = findViewById(R.id.checkBox); //设置是否选中(设置状态) checkBox.setChecked(false); //获取它的状态 boolean isChecked = checkBox.isChecked(); Log.d(TAG,"onCreate,isChecked:"+isChecked); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { Log.d(TAG,"onCreate,isChecked:"+isChecked); } }); } }
7.RadioButton
单选控件,可以和RadioGroup一起使用,只能选择一个。
<RadioGroup android:layout_width="match_parent" android:layout_height="match_parent" > <RadioButton android:id="@+id/radioButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="RadioButton" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="RadioButton" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="RadioButton" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="RadioButton" /> </RadioGroup>
8.ToggleButton
切换程序中的状态
两种状态
- android:textOn
- android:textOff
<ToggleButton android:id="@+id/toggleButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ToggleButton" android:textOn="hello" android:textOff="bye,bye"/>
9.SeekBar
<SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30" />
SeekBar seekBar = findViewById(R.id.seekBar); seekBar.setProgress(40); seekBar.setMax(100); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { Log.d(TAG,"onProgressChanged:"+i); } @Override public void onStartTrackingTouch(SeekBar seekBar) { Log.d(TAG,"onStartTrackingTouch:"+seekBar.getProgress()); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Log.d(TAG,"onStopTrackingTouch:"+seekBar.getProgress()); } });
10.OptionMenu
选项菜单是一个应用的主菜单用于放置对应用产生全局影响的操作。
在res文件夹中新建menu文件夹,创建option.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:title="保存" android:id="@+id/save" app:showAsAction="always" android:icon="@mipmap/ic_launcher"></item> <item android:title="设置" android:id="@+id/setting"></item> <item android:title="更多操作" > <menu > <item android:title="退出" android:id="@+id/exit"/> <item android:title="子菜单2" /> </menu> </item> </menu>
item中可以设置id,title以及icon图标等等。
showAsAction属性:
- always 直接在标题栏显示
- never 不显示
- ifRoom 有空间就显示
11.ContextMenu
<Button android:id="@+id/ctx_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上下文菜单" app:layout_constraintTop_toTopOf="parent"/>
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.注册 registerForContextMenu(findViewById(R.id.ctx_btn)); //2.创建 //3.菜单项操作 //4.上下文操作模式 //第一小点 实现接口ActionMode CallBack //第二小点 在View的长按事件启动上下文操作 findViewById(R.id.ctx_btn).setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { startActionMode(cb); return true; } }); } ActionMode.Callback cb = new ActionMode.Callback() { //创建,在启动上下文操作模式 @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { Log.e("TAG","创建"); getMenuInflater().inflate(R.menu.context,menu); return false; } //创建方法后调用 @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { Log.e("TAG","准备"); return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()){ case R.id.delete: Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_SHORT).show(); break; case R.id.name: Toast.makeText(MainActivity.this,"重命名",Toast.LENGTH_SHORT).show(); break; } return false; } //上下文操作模式结束时 @Override public void onDestroyActionMode(ActionMode mode) { Log.e("TAG","结束"); } }
12.PopupMenu
弹出菜单,一个模态形式展示的弹出风格的菜单绑在View上,一般出现在被绑定的View的下方。
final Button popupBtn = findViewById(R.id.popup_btn); findViewById(R.id.popup_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //实例化PopupMenu对象 PopupMenu menu = new PopupMenu(MainActivity.this,popupBtn); //加载菜单资源 利用MenuInlater menu.getMenuInflater().inflate(R.menu.popup,menu.getMenu()); //为PopupMenu设置点击监听器 menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()){ case R.id.copy: Toast.makeText(MainActivity.this,"复制",Toast.LENGTH_SHORT).show(); break; case R.id.paste: Toast.makeText(MainActivity.this,"粘贴",Toast.LENGTH_SHORT).show(); break; } return false; } }); //千万不要忘记 menu.show(); } });
|´・ω・)ノ