AS Layout布局

本文章来源于B站UP主(天哥在奔跑)的视频教学

线性布局LinearLayout

常用属性

  1. android:id 表示id
  2. android:layout_width 宽度
    • wrap_content:包含内容
    • match_parent:匹配父控件
    • 200dp:200个单位
  3. android:layout_height 高度
  4. android:background 背景
    • 000000:黑色
  5. android:layout_margin 外边距
    • android:layout_marginButtom
    • android:layout_marginTop
    • android:layout_marginRight
    • android:layout_marginLeft
    • android:layout_marginStart
    • android:layout_marginEnd
  6. android:layout_padding 内边距
    • android:layout_paddingButtom
    • android:layout_paddingTop
    • android:layout_paddingRight
    • android:layout_paddingLeft
    • android:layout_paddingStart
    • android:layout_paddingEnd
  7. android:orientation 布局方向
    • vertical:垂直方向
    • horizontal:水平方向
  8. android:gravity 对齐方式
    • buttom:底部对齐
    • top顶部对齐
    • center:居中
  9. android:weight 权重

相对布局RelativeLayout

常用属性

  1. android:layout_toLeftOf
  2. android:layout_toRightOf
  3. android:layout_alignButtom
  4. android:layout_alignParentButtom
  5. android:layout_below

TextView

常用属性

  1. android:text 文字内容
  2. android:textColot 文字颜色
  3. android:textSize 文字大小
    • 24sp:文字大小单位为sp
  4. android:maxLines 限制最大行数
  5. android:ellipsize 不够位置则显示...
    • marquee 跑马灯效果
  6. android:marqueeRepeatLimit 循环次数
    • marquee_forever 一直显示
  7. android:drawableRight 右边显示图标
  8. 设置中划线
    • TextView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG)
    • TextView.getPaint().setAntiAlias(true) 抗锯齿
  9. 设置下划线
    • TextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG)
  10. android:focusable 获得焦点
  11. android:focusableInTouchMode

Button

自定义背景形状

 设置形状
     设置颜色
    设置圆角
     描边

自定义点击效果


     按下时的效果
        
            
            
        
    
    
     没有按下时的效果
        
            
            
        
    

EditText

常用属性

  1. android:texxtAllCaps="false" 取消全部字母大写
  2. android:hint="XXX" 默认提示字符
  3. android:inputType 输入类型
    • textPassword 暗文密码
    • number 数字
  4. 内容改变监听器
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

常用属性

  1. 单选框

    
    ...
    

  1. android:checked="true" 默认选中
  2. android:button="@null" 取消默认样式
  3. 设置监听事件
RadioGrop.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group,  @IdRes int checkedId) {
        RadioButton radioButton = group.findViewById(checkedId);
        // do something...
    }
})

CheckBox复选框

常用属性

  1. xml

ImageView

常用属性

  1. android:src="" 图片内容
  2. android:scaleType 缩放形式
    • fitXY 撑满控件,宽高比可能发生改变
    • fitCenter 保持宽高比缩放,直至能够完全显示
    • centerCrop 保持宽高比缩放,直至完全覆盖控件,裁剪显示

加载网络图片

  1. 引入第三方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网格视图

常用属性

  1. android:numColumns 显示的列数
  2. android:gorizontalSpacing 垂直间距
  3. android:verticalSpacing 水平间距

ScrollView & HorizontalScrollView

常用属性

  1. ScrollView 垂直滚动视图
  2. HorizontalScrollView水平滚动视图
  3. xml

    
    
        ......
    

RecyclerView

常用属性

  1. 引入包
    在build.gradle中加入
dependencies {
    compile 'com.android.support:design:25.3.1'
}

布局文件



代码

  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));
    }
}
  1. 水平滚动布局
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();
    }
}));
  1. 网格布局
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();
    }
}));
  1. 瀑布流布局
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();
    }
}));
  1. 不同的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

  1. 加载网络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;
    }
}
  1. 加载assets下的html文件
    webview.loadUrl(file:///android_assets/test.html);
  2. 加载html代码
    webview.loadData();
    webview.loadDataWithBaseURL();
  3. 网页的前进后退
    webview.canGoBack();
    webview.goBack();
    webview.canGoForward();
    webview.goForward();
    webview.canGoBackOrForward(int steps);
    webview.goBackOrForward(int steps);
  4. 按下返回键时使网页后退而不是退出当前Activity
// 重写onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if((keyCode == KeyEvent.KEYCODE_BACK) && (webView.canGoBack())) {
        webView.goBack();
        return true;
    }
}

Toast

  1. 默认
Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_LONG).show();
  1. 改变位置
Toast toastCenter = Toast.makeText(getApplicationContext(), "居中Toast", Toast.LONG);
toastCenter.setGravity(Gravity.CENTER, 0, 0);
toastCenter.show();
  1. 带图片
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(警示对话框)

  1. 默认样式
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();
  1. 单选样式
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();
  1. 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();
  1. 多选样式
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();
  1. 自定义样式
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

常用属性

  1. style="@android:style/Widget.XXX" 样式
    2.android:visibility
    gone 移除
    visible 可见
    invisible 不可见
  2. android:max 最大值
  3. android:progress 当前值
  4. android:secondaryProgress 二级进度值
  5. style="@style/MyProgressBar" 自定义进度条背景






代码

// 设置当前进度值
ProgressBar.setProgress(30);

ProgressDialog

代码

  1. 样式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();
  1. 样式2(横向进度条)
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

自定义Dialog

常用属性

  1. 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); // 在某个控件下方显示

你可能感兴趣的:(AS Layout布局)