常用布局
1.线性布局 LinearLayout
orientation:方向;vertical,垂直;horizontal,水平 gravity:对齐方式,子控件相对于当前控件的对齐方式 layout_gravity:当前控件相对于父控件的对齐方式 margin:当前控件相对于四周的间距。 padding:当前控件中的子控件相对于当前控件四周的间距。2.相对布局 RelativeLayout 都是从左上角开始布局,要控制位置需要设置每个控件相对于其他控件的位置。
layout_below:位于哪个控件的下方 layout_above:位于哪个控件的上方 layout_toLeftOf:指定当前控件位于哪个控件的左边 layout_toRightOf:指定当前控件位于哪个控件的右边 layout_alignParentRight:当前控件基于父窗体的对其方式 layout_centerHorizontal:水平居中 layout_centerVertical:垂直居中 layout_centerInParent:位于父窗体的中间福
3.帧布局 FrameLayout帧布局中的子控件都是一层一层向上叠加的。
常用控件
设置背景图 android:background="@drawable/zxxsh" 图片放到,图片名字一定要是小写字母、下划线,数量尽量不要加
文本显示
选择框
//设置选择框图位置 生成步骤如下
按钮
Button fragment_btn_liJiZhiFu = (Button)findViewById(R.id.fragment_btn_liJiZhiFu);fragment_btn_liJiZhiFu.setOnClickListener(this);
继承它View.OnClickListener
public class Activity1 extends Activity implements View.OnClickListener { private View fragment2; private ListvipList;
//点击事件@Overridepublic void onClick(View view) { switch (view.getId()) { case R.id.fragment_btn_liJiZhiFu://支付 Toast.makeText(getActivity(),"请选择支付方式",Toast.LENGTH_SHORT).show(); break; }}
GridView这个是自定义GridView 为啥自定义呢,这个是滑动控件 如同一个布局有Scrollview的话他会冲突,所有要自定义
自定义代码
public class MyGridView extends GridView { public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}
//点击事件 class ItemClick implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView adapterView, View view, int i, long l) { position = i;// vipList.get(i).getGoodsName()+"\n"+String.valueOf(vipList.get(i).getPrice( goodPrice = String.valueOf(vipList.get(i).getPrice()); goodeNome = String.valueOf(vipList.get(i).getGoodsName()); } } class MyAdapter extends BaseAdapter { @Override public int getCount() { if (vipList == null) { return 0; } return vipList.size(); } @Override public Object getItem(int i) { return vipList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder; if (view == null) { //fragment2_img_huiYuanKa viewHolder = new ViewHolder(); view = UIUtils.inflate(R.layout.fragment2_hui_yuan_ka_item); viewHolder.fragment2_tv_vipCordPresent = (TextView) view.findViewById(R.id.fragment2_tv_vipCordPresent); view.setTag(viewHolder); } else { viewHolder = (ViewHolder)view.getTag(); } d = vipList.get(i).getPrice(); String prict = df.format(d); viewHolder.fragment2_tv_vipCordPresent.setText(vipList.get(i).getGoodsName()+"\n"+prict+"元"); return view; } } private class ViewHolder { public TextView fragment2_tv_vipCordPresent; }
MyGridView fragment2_mgr_vipCard = (MyGridView)fragment2.findViewById(R.id.fragment2_mgr_vipCard);myAdapter = new MyAdapter();fragment2_mgr_vipCard.setAdapter(myAdapter);fragment2_mgr_vipCard.setOnItemClickListener(new ItemClick());
myAdapter.notifyDataSetChanged();//刷新
ListView用法基本相同 下面是自定义的
public class MyListView extends ListView { public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }// public MyListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {// super(context, attrs, defStyleAttr, defStyleRes);// } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}
如果要跳转别的界面吧他放到按钮方法中
Intent intent = new Intent(getActivity(), LoginActivity.class);startActivity(intent);
//跳转时如果上个界面不用的话.关闭
finish();
A->B 跳转顺传
A
Intent intent = new Intent(this,FinalActivity.class);
//把名字和性别传过去 intent.putExtra("userName",userName); intent.putExtra("sex",sex);startActivity(intent);
B
//开启Activity的意图对象
Intent intent = getIntent(); //获取携带过来的数据 String name = intent.getStringExtra("userName");int sex = intent.getIntExtra("sex",0);
B->A 逆传
//设置意图对象 Intent intent = new Intent(); intent.putExtra("smscontent","jjj"); //返回给主界面
setResult(20,intent);//20作为区分标识 resultCode
finish();
在主界面会调用这个方法
//接受界面
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == 20) { String msg = data.getStringExtra("smscontent"); System.out.println(msg); et_messageText.setText(msg); }else if (resultCode == 10) { et_inpnub.setText(data.getStringExtra("phone")); } super.onActivityResult(requestCode, resultCode, data); }
权限
compile 'com.yanzhenjie:permission:1.1.0'
图片轮播
compile 'com.youth.banner:banner:1.4.10'
compile 'com.github.bumptech.glide:glide:3.7.0'
//权限
AndPermission.with(this) .requestCode(100) .permission( Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE ) .rationale(rationaleListener) .callback(listener) .start();
//成功回调private PermissionListener listener = new PermissionListener() { @Override public void onSucceed(int requestCode, @NonNull ListgrantPermissions) { if (requestCode == 100) { } } //如果用户点击拒绝,以后不在提示后将会调用的 @Override public void onFailed(int requestCode, @NonNull List deniedPermissions) { if (requestCode == 100) { if (AndPermission.hasAlwaysDeniedPermission(getActivity(), deniedPermissions)) { // 第一种:用AndPermission默认的提示语。 AndPermission.defaultSettingDialog(getActivity()).show(); } } }};//拒绝后会调用private RationaleListener rationaleListener = new RationaleListener() { @Override public void showRequestPermissionRationale(final int requestCode, final Rationale rationale) { com.yanzhenjie.alertdialog.AlertDialog.newBuilder(getActivity()) .setTitle("友好提醒") .setMessage("你已拒绝过定位权限,沒有定位定位权限无法为你推荐附近的妹子,你看着办!") .setPositiveButton("给你权限", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { rationale.resume(); } }) .setNegativeButton("我拒绝", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { rationale.cancel(); } }) .show(); }};
图片轮播
public class GlideImageLoader extends ImageLoader { @Override public void displayImage(Context context, Object path, ImageView imageView) { //具体方法内容自己去选择,次方法是为了减少banner过多的依赖第三方包,所以将这个权限开放给使用者去选择 Glide.with(context.getApplicationContext()) .load(path) .crossFade() .into(imageView); }
frgment1_banner = (Banner) fragmentView.findViewById(R.id.frgment1_banner);
ArrayListbannerImageUrl = new ArrayList (); for (int i = 0; i < model.getObj().size(); i++) { bannerImageUrl.add(Confing.imageRootUrl+model.getObj().get(i).getAdCode()); } frgment1_banner.setImages(bannerImageUrl) .setDelayTime(3000) .setImageLoader(new GlideImageLoader())// .setOnBannerListener(HomeFragment.this) .setBannerAnimation(Transformer.Default) .updateBannerStyle(BannerConfig.NUM_INDICATOR); frgment1_banner.start();
//图片轮播如果从文件或internet加载图像