本文章来源于B站UP主(天哥在奔跑)的视频教学
线性布局LinearLayout
常用属性
- android:id 表示id
- android:layout_width 宽度
- wrap_content:包含内容
- match_parent:匹配父控件
- 200dp:200个单位
- android:layout_height 高度
- android:background 背景
- 000000:黑色
- android:layout_margin 外边距
- android:layout_marginButtom
- android:layout_marginTop
- android:layout_marginRight
- android:layout_marginLeft
- android:layout_marginStart
- android:layout_marginEnd
- android:layout_padding 内边距
- android:layout_paddingButtom
- android:layout_paddingTop
- android:layout_paddingRight
- android:layout_paddingLeft
- android:layout_paddingStart
- android:layout_paddingEnd
- android:orientation 布局方向
- vertical:垂直方向
- horizontal:水平方向
- android:gravity 对齐方式
- buttom:底部对齐
- top顶部对齐
- center:居中
- android:weight 权重
相对布局RelativeLayout
常用属性
- android:layout_toLeftOf
- android:layout_toRightOf
- android:layout_alignButtom
- android:layout_alignParentButtom
- android:layout_below
TextView
常用属性
- android:text 文字内容
- android:textColot 文字颜色
- android:textSize 文字大小
- 24sp:文字大小单位为sp
- android:maxLines 限制最大行数
- android:ellipsize 不够位置则显示...
- marquee 跑马灯效果
- android:marqueeRepeatLimit 循环次数
- marquee_forever 一直显示
- android:drawableRight 右边显示图标
- 设置中划线
TextView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG)
-
TextView.getPaint().setAntiAlias(true)
抗锯齿
- 设置下划线
TextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG)
- android:focusable 获得焦点
- android:focusableInTouchMode
Button
自定义背景形状
设置形状
设置颜色
设置圆角
描边
自定义点击效果
- 按下时的效果
- 没有按下时的效果
EditText
常用属性
- android:texxtAllCaps="false" 取消全部字母大写
- android:hint="XXX" 默认提示字符
- android:inputType 输入类型
- textPassword 暗文密码
- number 数字
- 内容改变监听器
EditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharWequence s, int start, int count, in after) {
// do something...
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// do something...
}
@Override
public void afterTextChanged(Editable s) {
// do something...
}
})
RadioButton
常用属性
- 单选框
...
- android:checked="true" 默认选中
- android:button="@null" 取消默认样式
- 设置监听事件
RadioGrop.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton radioButton = group.findViewById(checkedId);
// do something...
}
})
CheckBox复选框
常用属性
- xml
ImageView
常用属性
- android:src="" 图片内容
- android:scaleType 缩放形式
- fitXY 撑满控件,宽高比可能发生改变
- fitCenter 保持宽高比缩放,直至能够完全显示
- centerCrop 保持宽高比缩放,直至完全覆盖控件,裁剪显示
加载网络图片
- 引入第三方jar包 (github:bumptech/glide)
imageview = (ImageView) findViewById(R.id.iv_4);
Glide.with(this).load("http.....").into(imageview);
ListView列表视图
public class MyListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflatter mLayoutInflater;
public MyListAdapter(Context context) {
this.mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return 10;
}
@Override
public Object getItem(int position) {
return null
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolder {
public ImageView imageView;
public TextView tvTitile, tvTime, tvContent;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.layout_list_item, null);
holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.iv);
holder.tvTitile = convertView.findViewById(R.id.tv_titile);
holder.tvTime = convertView.findViewById(R.id.tv_time);
holder.tvContent = convertView.findViewById(R.id.tv_content);
convertView.setTay(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
//给控件赋值
holder.tvTitle.setText("XXXX");
holder.tvTime.setText("XXXX");
holder.tvContent.setText("XXXX");
Glide.with(mContext).load("").into(holder.imageView);
return convertView;
}
}
ListView.setAdapter(new MyListAdapter(ListViewActivity.tthis));
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Toast.makeText(GridViewActivity.this, "点击 pos"+position, Toast.LENGTH_SHORT).show();
}
})
ListView.setOnItemLongClickLIstener();
GridView网格视图
常用属性
- android:numColumns 显示的列数
- android:gorizontalSpacing 垂直间距
- android:verticalSpacing 水平间距
ScrollView & HorizontalScrollView
常用属性
- ScrollView 垂直滚动视图
- HorizontalScrollView水平滚动视图
- xml
......
RecyclerView
常用属性
- 引入包
在build.gradle中加入
dependencies {
compile 'com.android.support:design:25.3.1'
}
布局文件
代码
- 垂直滚动布局
// Adapter
public class LinearAdapter extends RecyclerView.Adapter {
private Context mContext;
private OnItemClickListener mlistener;
public LinearAdapter(Context context, OnItemClickListener listener) {
this.mContext = context;
this.mlistener = listener;
}
@Override
public LinearAdapter.LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.XXX, parent, false));
}
@Override
public void onBindViewHolder(LinearAdapter.LinearViewHolder holder, int position) {
holder.textView.setText("Hello World!!!");
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mlistener.onClick(position);
}
});
}
@Override
public int getItemCount() {
return 30;
}
class LinearViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public LinearViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.XXX);
}
}
// 实现点击事件的接口
public interface OnItemClickListener {
void onClick(int pos);
}
}
RecyclerView.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this));
// 分割线功能
RecyclerView.addItemDecoration(new MyDecoration());
RecyclerView.setAdapter(new LinearAdapter(LinearRecyclerViewActivity.this, new LinearAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(LinearRecyclerViewActivity.this, "click"+pos, Toast.LENGTH_SHORT).show();
}
}));
class MyDecoration extends RecyclerView.ItemDecoration {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0,0,0,getResource().getDimensionPixelOffset(R.dimen.dividerHeight));
}
}
- 水平滚动布局
RecyclerView.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this));
RecyclerView.setOrientation(LinearLayoutManager.HORIZONTAL);
RecyclerView.setAdapter(new LinearAdapter(HorRecyclerViewActivity.this, new HorAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(HorRecyclerViewActivity.this, "click"+pos, Toast.LENGTH_SHORT).show();
}
}));
- 网格布局
RecyclerView.setLayoutManager(new GridLayoutManager(GridRecyclerViewActivity.this, 3));
RecyclerView.setAdapter(new GridAdapter(GridRecyclerViewActivity.this, new GridAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(HorRecyclerViewActivity.this, "click"+pos, Toast.LENGTH_SHORT).show();
}
}));
- 瀑布流布局
RecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
RecyclerView.setAdapter(new StaggeredGridAdapter(PuRecyclerViewActivity.this, new StaggeredGridAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(HorRecyclerViewActivity.this, "click"+pos, Toast.LENGTH_SHORT).show();
}
}));
- 不同的ViewHolder
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == 0)
{
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.id.XXX, parent, false));
}else {
return new LinearViewHolder2(LayoutInflater.from(mContext).inflate(R.id.XXX, parent, false));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(getItemViewType(position) == 0)
{
((LinearViewHolder)holder).textView.setText("Hello World!!!");
}else {
((LinearViewHolder2)holder).textView.setText("XXX");
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mlistener.onClick(position);
}
});
}
// 通过不同的ViewType实例化不同的ViewHolder
@Override
public int getItemViewType(int position) {
if(position % 2 == 0) {
return 0;
}else {
return 1;
}
}
class LinearViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public LinearViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.XXX);
}
}
class LinearViewHolder2 extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView;
public LinearViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.XXX);
imageView = (ImageView) itemView.findViewById(R.id.XXX);
}
}
WebView
- 加载网络URL
webview.getSettings().setJavaScriptEnable(true);
// 继续在本应用打开链接而不是跳转外部浏览器
webview.setWebViewClient(new MyWebViewClient());
webview.loadUrl("http://m.baidu.com");
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
}
- 加载assets下的html文件
webview.loadUrl(file:///android_assets/test.html);
- 加载html代码
webview.loadData();
webview.loadDataWithBaseURL();
- 网页的前进后退
webview.canGoBack();
webview.goBack();
webview.canGoForward();
webview.goForward();
webview.canGoBackOrForward(int steps);
webview.goBackOrForward(int steps);
- 按下返回键时使网页后退而不是退出当前Activity
// 重写onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && (webView.canGoBack())) {
webView.goBack();
return true;
}
}
Toast
- 默认
Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_LONG).show();
- 改变位置
Toast toastCenter = Toast.makeText(getApplicationContext(), "居中Toast", Toast.LONG);
toastCenter.setGravity(Gravity.CENTER, 0, 0);
toastCenter.show();
- 带图片
Toast toastCustom = new Toast;
LayoutInflater inflater = new LayoutInflater.from(ToastActivity.this);
View view = inflater.inflater(R.layout.XXX, null);
ImageView imageView = view.findViewById(R.id.XXX);
TextView textView = view.findViewById(R.id.XXX);
imageView.setImageResource(R.drawable.XXX);
textView.setText("XXX");
toastCustom.setView(view);
toastCustom.show();
AlertDialog(警示对话框)
- 默认样式
AlerDialog.Builder builder = new AlerDialog.Builder(DialogActivity.this);
builder.setTitle("XXX");
builder.setIcon(R.drawable.XXX);
builder.setMeesage("XXX");
// 积极的:setPositiveButton
// 中立的:setNeutralButton
// 消极的:setNegativeButton
builder.setPositiveButton("X", new DialogInterface.OnClickListener() {
// do something...
})
builder.show();
- 单选样式
final String[] array1 = String[]{"男", "女"};
AlerDialog.Builder builder1 = new AlerDialog.Builder(DialogActivity.this);
builder1.setTitle("选择性别");
builder1.setItem(array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something...
}
});
builder1.show();
- radio button 样式
final String[] array2 = String[]{"男", "女"};
AlerDialog.Builder builder2 = new AlerDialog.Builder(DialogActivity.this);
builder2.setSingleChoiceItems(array2, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something...
// 点击时消失
dialog.dismiss();
}
});
// 点击提示框外部区域也不能使提示框消失
builder2.setCancelable(false);
builder2.show();
- 多选样式
final String[] array3 = String[]{"唱歌", "跳舞", "写代码"};
boolean[] isSeletced = new boolean[]{false, false, true};
AlerDialog.Builder builder3 = new AlerDialog.Builder(DialogActivity.this);
builder3.setTitle("选择兴趣");
builder3.setMultiChoiceItems(array3, isSeletced, new DialogInterface.OnMultiChoiceClickListener {
@Override
public void OnClick(DialogInterface dialog, int which, boolean isChecked) {
// do something...
}
});
builder3.show();
- 自定义样式
AlerDialog.Builder builder4 = new AlerDialog.Builder(DialogActivity.this);
View view = LayoutInfalter.from(DialogActivity.this).inflate(R.layout.XXX);
builder4.setView(view);
builder4.show();
ProgressBar & ProgressDialog
ProgressBar
常用属性
-
style="@android:style/Widget.XXX"
样式
2.android:visibility
gone 移除
visible 可见
invisible 不可见 -
android:max
最大值 -
android:progress
当前值 -
android:secondaryProgress
二级进度值 -
style="@style/MyProgressBar"
自定义进度条背景
代码
// 设置当前进度值
ProgressBar.setProgress(30);
ProgressDialog
代码
- 样式1(转圈)
ProgressDialog progressDialog = new
ProgressDialog(ProgressActivity.this);
progressDialog.setTitle("提示");
progressDialog.setMessage("正在加载");
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
ToastUtil.showMsg(ProgressActivity.this, "加载完成");
}});
//progressDialog.setCancelable(false); // 只有加载完成才能消失
progressDialog.show();
- 样式2(横向进度条)
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
自定义Dialog
常用属性
- android:textStyle
"bold" 加粗
PopupWindow
View view = getLayoutInflater().inflater(R.layout.XXX);
PopupWindow mPop = new PopupWindow(view, width, ViewGroup.LayoutParams.WARP_CONTENT);
mPop.setOutsideTouchable(true); // 点击其他区域时消失
mPop.setFocusable(true); // 第一次点击弹出,第二次点击收起
mPop.showAsDropDwon(mBtnPop); // 在某个控件下方显示