当前位置:   article > 正文

Android进阶:网络与数据存储—步骤1:Android网络与通信(第7小节:CadView)_android otaliastudios cameraview依赖网络吗?

android otaliastudios cameraview依赖网络吗?

内容概要:

  • CardView基础
  1. CardView介绍
  2. CardVie常用属性
  3. CardView属性效果展示
  • CardView案例实现
  1. CardVie基本操作
  2. 案例-布局搭建
  3. 案例-实体类创建
  4. 案例-功能实现
  5. 案例-适配
  6. CardView开发注意事项

一、CardView基础

1、CardView介绍

CardView是什么?

  1. Android5.0之后新增
  2. com.android.support:cardview-v7:26.1.0独立引用
  3. 继承自FragmeLayout,方便作为其他控件的容器,添加3D阴影和圆角效果

2.CardVie常用属性

  1. cardBackgroundColor 设置背景色

  2. cardCornerRadius 设置圆角半径

  3. contentPadding 设置内部padding

  4. cardElevation 设置阴影大小

  5. cardUseCompatPadding 默认为false, 用于5.0及以上,true则添加额外的padding绘制阴影

  6. cardPreventCornerOverlap 默认为true, 用于5.0以下,添加额外的padding,防止内容和圆角重叠

  • 示例:

代码演示:

首先在build.gradle添加依赖包

    implementation 'com.android.support:cardview-v7:26.1.0'

最简单的应用给textView设置一个阴影效果:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <android.support.v7.widget.CardView
  9. android:layout_gravity="center"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:layout_width="200dp"
  14. android:layout_height="50dp"
  15. android:text="Hello World!"
  16. android:gravity="center"
  17. />
  18. </android.support.v7.widget.CardView>
  19. </FrameLayout>

 2.案例-布局搭建

在写布局文件的时候,比如text的初始值我们可以设置一个随意的文本,但是我们只想自己测试预览看到

不想被意外的显示在App上,可以在主View上加tools

    xmlns:tools="http://schemas.android.com/tools"

然后再相应你想测试的值预览的地方加上

  1. //这个预览和在app都可以看到
  2. android:text="Hello World!"
  3. //这个只能预览效果,app上不显示内容
  4. tools:text="Hello World!"

item_msg.xml CardView的布局 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <android.support.v7.widget.CardView
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginBottom="8dp"
  12. android:layout_marginLeft="16dp"
  13. android:layout_marginRight="16dp"
  14. android:layout_marginTop="8dp"
  15. app:cardCornerRadius="10dp"
  16. app:cardElevation="6dp">
  17. <LinearLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:orientation="vertical">
  21. <ImageView
  22. android:id="@+id/id_image_cardview"
  23. android:layout_width="match_parent"
  24. android:layout_height="150dp"
  25. android:scaleType="centerCrop"
  26. tools:background="@drawable/img01" />
  27. <TextView
  28. android:id="@+id/id_title_cardview"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_margin="8dp"
  32. android:text="Hello World!"
  33. tools:text="aaaaaa" />
  34. <TextView
  35. android:id="@+id/id_content_cardview"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:layout_marginBottom="8dp"
  39. android:layout_marginLeft="8dp"
  40. android:layout_marginRight="8dp"
  41. android:text="Hello World!"
  42. tools:text="aaaaaa" />
  43. </LinearLayout>
  44. </android.support.v7.widget.CardView>
  45. </FrameLayout>

ListView显示CardView

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <ListView
  9. android:id="@+id/listview"
  10. android:background="#ffffff"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent">
  13. </ListView>
  14. </LinearLayout>

3案例-实体类创建

Msg.java

  1. /**
  2. * 信息封装类
  3. */
  4. public class Msg {
  5. private int Id;
  6. private int ImgId;
  7. private String Tilte;
  8. private String Content;
  9. public Msg(int id, int imgId, String tilte, String content) {
  10. Id = id;
  11. ImgId = imgId;
  12. Tilte = tilte;
  13. Content = content;
  14. }
  15. public int getId() {
  16. return Id;
  17. }
  18. public void setId(int id) {
  19. Id = id;
  20. }
  21. public int getImgId() {
  22. return ImgId;
  23. }
  24. public void setImgId(int imgId) {
  25. ImgId = imgId;
  26. }
  27. public String getTilte() {
  28. return Tilte;
  29. }
  30. public void setTilte(String tilte) {
  31. Tilte = tilte;
  32. }
  33. public String getContent() {
  34. return Content;
  35. }
  36. public void setContent(String content) {
  37. Content = content;
  38. }
  39. }

MsgLab.java 

  1. /**
  2. * 信息的类添加
  3. */
  4. public class MsgLab {
  5. private List<Msg> list;
  6. public static List<Msg> generateList(){
  7. List<Msg> msgList=new ArrayList<>();
  8. Msg msg = new Msg(1,
  9. R.drawable.img01,
  10. "如何才能不错过人工智能的时代?",
  11. "下一个时代就是机器学习的时代,慕课网发大招,与你一起预见未来!");
  12. msgList.add(msg);
  13. msg = new Msg(2,
  14. R.drawable.img02,
  15. "关于你的面试、实习心路历程",
  16. "奖品丰富,更设有参与奖,随机抽取5名幸运用户,获得慕课网付费面试课程中的任意一门!");
  17. msgList.add(msg);
  18. msg = new Msg(3,
  19. R.drawable.img03,
  20. "狗粮不是你想吃,就能吃的!",
  21. "你的朋友圈开始了吗?一半秀恩爱,一半扮感伤!不怕,还有慕课网陪你坚强地走下去!!");
  22. msgList.add(msg);
  23. msg = new Msg(4,
  24. R.drawable.img04,
  25. "前端跳槽面试那些事儿",
  26. "工作有几年了,项目偏简单有点拿不出手怎么办? 目前还没毕业,正在自学前端,请问可以找到一份前端工作吗,我该怎么办?");
  27. msgList.add(msg);
  28. msg = new Msg(5,
  29. R.drawable.img05,
  30. "图解程序员怎么过七夕?",
  31. "哈哈哈哈,活该单身25年!");
  32. msgList.add(msg);
  33. return msgList;
  34. }
  35. }

4.案例-功能实现

  1. public class MainActivity extends AppCompatActivity {
  2. private ListView listView;
  3. private List<Msg> msgs=new ArrayList<>();
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. listView=findViewById(R.id.listview);
  9. msgs.addAll(MsgLab.generateList());//给msgs添加数据
  10. msgs.addAll(MsgLab.generateList());//给msgs添加数据
  11. listView.setAdapter(new MsgAdapter(MainActivity.this,msgs));
  12. }
  13. }

实现效果:

CardView屏幕适配

一、手机适配之 dimen 基础知识

dimen 是用来定义尺寸的资源文件,默认路径:工程的 res\values\dimens.xml

二、dimen 定义的尺寸资源文件作用?

可以在 res 下创建不同分辨率的 values 目录,例如 values-480x320、values-800x480、 values-1920x1080、vaule-21是android5.0 等,并且在上述目录中可以分别创建尺寸文件,这样在不同分辨率 下,该目录下的 dimens.xml 会代替 res/values/dimens.xml 达到最佳的适配效果。

三、dimen 定义的资源文件如何使用?

1、 在工程的 res\values\目录下创建一个 dimens.xml 尺寸资源文件 <?xml version="1.0" encoding="utf-8"?>

  1. <resources>
  2. <dimen name="btn_width">200px</dimen>
  3. <dimen name="btn_height">200px</dimen> </resources>

2、 添加一个布局文件,在此布局文件中添加一个按钮,使用尺寸资源文件来定义按钮 的宽和高

  1. <?xml version="1.0" encoding="utf-8"?> <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <Button
  5. android:layout_width="@dimen/btn_width"
  6. android:layout_height="@dimen/btn_height"
  7. android:text="@string/app_name" />
  8. </LinearLayout>

3、在 java 代码中也可以获取到 dimens 尺寸资源文件的数值

  1. Resources res = getResources();
  2. float btn_h = res.getDimension(R.dimen.btn_height); float btn_w = res.getDimension(R.dimen.btn_width);

四、尺寸文件使用建议

1、在 values 目录下创建一个默认的 dimens 文件

2、尽可能多的创建不同分辨率的 dimens 文件(这样应用才会适配的完美)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/910245
推荐阅读
相关标签
  

闽ICP备14008679号