Android读书笔记(二)

来源:互联网 发布:linux 内核 视频 编辑:程序博客网 时间:2024/05/22 13:01

1.百分比布局PercentFrameLayout与PercentRelativeLayout
PercentFrameLayout与PercentRelativeLayout的百分比设置一样,在这里只说PercentFrameLayout。
要使用这种布局需要在app/build.gradle文件中的dependencies下进行添加。

dependencies {    compile 'com.android.support:percent:24.2.1'}

在布局文件中进行使用,设置百分比宽高需要app属性来设置,如下。

        app:layout_widthPercent="50%"        app:layout_heightPercent="30%"

代码、界面展示

<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_gravity="center_horizontal"        android:text="宽度50% 高度30%"        android:textSize="20sp"        app:layout_heightPercent="30%"        app:layout_widthPercent="50%" />    <Button        android:layout_gravity="center"        android:text="宽度60%,高度10%"        android:textSize="20sp"        app:layout_heightPercent="10%"        app:layout_widthPercent="60%" />    <Button        android:layout_gravity="bottom|center_horizontal"        android:text="宽度80%,高度40%"        android:textSize="20sp"        app:layout_heightPercent="40%"        app:layout_widthPercent="80%" /></android.support.percent.PercentFrameLayout>

这里写图片描述

2.RecyclerView的使用
此控件可以纵向滚动,也可以横向滚动,增强版的ListView。要想使用此控件必须添加依赖的库。

/*app/build.gradle*/dependencies {    compile 'com.android.support:recyclerview-v7:24.2.1'}

在布局文件中的使用

    <android.support.v7.widget.RecyclerView        android:id="@+id/rv"        android:layout_width="match_parent"        android:layout_height="match_parent">    </android.support.v7.widget.RecyclerView>

创建适配器,在适配器中创建内部类ViewHolder并使其继承RecyclerView.ViewHolder。重写适配器中onCreateViewHolder()、onBindViewHolder()和getItemCount()方法。onCreateViewHolder()中主要工作是创建ViewHolder、加载布局、设置事件。onBindViewHolder()中主要工作是对控件进行赋值。getItemCount()用来返回数据的数量。

public class VtsAdapter extends RecyclerView.Adapter<VtsAdapter.ViewHolder> {    private List<Vegetables> vegetables;    //获取数据    public VtsAdapter(List<Vegetables> vegetables) {        this.vegetables = vegetables;    }    static class ViewHolder extends RecyclerView.ViewHolder {        ImageView vImage;        TextView vName;        View vView;        //获取RecyclerView子项中的实例        public ViewHolder(View itemView) {            super(itemView);            vView = itemView;            vImage = (ImageView) itemView.findViewById(R.id.image_v_iv);            vName = (TextView) itemView.findViewById(R.id.name_v_tv);        }    }    @Override    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = LayoutInflater.from(parent.getContext())                .inflate(R.layout.item_v, parent, false);        final ViewHolder holder = new ViewHolder(view);        holder.vView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int position = holder.getAdapterPosition();                Vegetables vts = VtsAdapter.this.vegetables.get(position);                Toast.makeText(v.getContext(),vts.getName()+"整体",Toast.LENGTH_SHORT).show();            }        });        holder.vName.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int position = holder.getAdapterPosition();                Vegetables vts = vegetables.get(position);                Toast.makeText(v.getContext(), vts.getName(), Toast.LENGTH_SHORT).show();            }        });        return holder;    }    @Override    public void onBindViewHolder(ViewHolder holder, int position) {        Vegetables vts = vegetables.get(position);        holder.vImage.setImageResource(vts.getImagesId());        holder.vName.setText(vts.getName());    }    @Override    public int getItemCount() {        return vegetables.size();    }}

使用RecyclerView来进行显示数据,与ListView不同的是需要设置layoutManager,下面代码中使用了LinearLayoutManager ,是线性布局,与ListView的效果差不多。

        RecyclerView rv = (RecyclerView) this.findViewById(R.id.rv);        LinearLayoutManager layoutManager = new LinearLayoutManager(this);        rv.setLayoutManager(layoutManager);        VtsAdapter vtsAdapter = new VtsAdapter(vegetables);        rv.setAdapter(vtsAdapter);

效果图:
这里写图片描述

接下来,说一说ListView不能实现的效果,横向滚动与瀑布流。要实现横向滚动,需要在item布局文件中指定排列方向为垂直,指定布局宽度。

<!--item_v.xml--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="100dp"    android:layout_height="wrap_content"    android:orientation="vertical">    <ImageView        android:id="@+id/image_v_iv"        android:layout_width="40dp"        android:layout_height="40dp"        android:layout_gravity="center_horizontal"        android:src="@mipmap/ic_launcher" />    <TextView        android:id="@+id/name_v_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="test"        android:textSize="25sp" /></LinearLayout>

再对LinearLayoutManager 设置一下就可以了。

 layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

效果图:
这里写图片描述

瀑布流效果的实现,修改item布局文件如下:

<!--在瀑布流布局中宽度自动适配--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="5dp"    android:orientation="vertical">    <ImageView        android:id="@+id/image_v_iv"        android:layout_width="60dp"        android:layout_height="60dp"        android:layout_gravity="center_horizontal"        android:src="@mipmap/ic_launcher" />    <TextView        android:id="@+id/name_v_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="left"        android:text="test"        android:textSize="25sp" /></LinearLayout>

修改代码,使用StaggeredGridLayoutManager。

 RecyclerView rv = (RecyclerView) this.findViewById(R.id.rv); //参数一:列数 参数二:排列方向 StaggeredGridLayoutManager sglm =              new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL); rv.setLayoutManager(sglm);

这里为了显示出瀑布流的效果,增加了名字的长度。效果图:
这里写图片描述

3.Nine-Patch图片
可以对图片进行局部的拉伸或不拉伸。
看图比较直观,这是一个被均匀拉伸的图片,显示成这样大家肯定不喜欢。
这里写图片描述
为了处理这样的情况,我们需要用Nine-Patch来对图片进行处理(在我学Nine-Patch图片时,使用的是draw9patch.bat)。在android studio中右键图片,点击“Create 9-Patch file…”,创建了一个“名字.9.png”的图片(不要改xx.9.png的命名格式,改了就和普通的图片一样了),这就是我们所要的图片,双击,就能进行编辑了。
点击“Create 9-Patch file...”

打开后的图片

处理图片,对上下左右四个小条拖拽来设置。上边和左边表示图片拉伸区域,右边和下边表示内容区域。设置完成,如图:
这里写图片描述

好看多了

有内容的图片