11月9日Android学习笔记

标签

Android

ScrollView

布局加载器

Fragment

CircleImageView

发布时间:

本文字数:434 字 阅读完需:约 3 分钟

scrollView 滑动视图

在ScrollView当中只能有一个子控件

布局加载器的获取

两种方式

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        LayoutInflater inflater = getLayoutInflater();

将view对象添加到listview的头布局

        ListView lv;
        lv.addHeaderView(headerView);

示例:设置头布局和尾部局

    private void addHeaderAndFooterView() {
        //将布局文件转换成view对象
//        LayoutInflater layoutInflater = LayoutInflater.from(this);
        LayoutInflater inflater = getLayoutInflater();
        View headerView = inflater.inflate(R.layout.item_lv_header, null);
        lv.addHeaderView(headerView);
        //查找头布局的控件
        TextView tv1 = headerView.findViewById(R.id.header_tv1);
        TextView tv2 = headerView.findViewById(R.id.header_tv2);
        ImageView iv = headerView.findViewById(R.id.header_iv);
        tv1.setText(dao.getName());
        tv2.setText(dao.getDate());
        //通过asset工具类读取图片数组
        String fname= "xzcontentlogo/" + dao.getLogoname() + ".png";
        byte[] array = AssetsUtils.getInstance(this).getAssetByteArray(fname);
        Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0, array.length);
        iv.setImageBitmap(bitmap);
        View footerview = inflater.inflate(R.layout.item_footer, null);
        lv.addFooterView(footerview);
        TextView ft = findViewById(R.id.footer_tv2);
        ft.setText(dao.getInfo());


    }

Fragment(活动的碎片)

fragment嵌入页面中,将Activity拆分为多个部分

在activity中加载StartFragment碎片

注意,引入的fragment包要与fragmentManager对应的包头一致

//在activity当中加载碎片的过程
//1. 获取Fragment对象
starFragment = new StarFragment();
//2. 获取碎片管理者
manager = getSupportFragmentManager();
//3. 获取碎片事务对象
FragmentTransaction transaction = manager.beginTransaction();
//4. 发布任务
transaction.add(R.id.r2_center, starFragment);
//5. 提交任务
transaction.commit();

fragment示例

package com.xyl.app1108.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xyl.app1108.R;


public class StarFragment extends Fragment {

    /**
     * 创建fragment绑定显示的view视图
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_star, container, false);
        return view;
    }
}

点击radioButton切换fragment示例

 RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int id) {
            transaction = manager.beginTransaction();
            switch (id) {
                case R.id.r2_rb1:
                    titleTv.setText("星座");
                    transaction.hide(luckyFragment);
                    transaction.hide(partnerFragment);
                    transaction.hide(meFragment);
                    transaction.show(starFragment);
                    transaction.commit();
                    break;
                case R.id.r2_rb2:
                    titleTv.setText("配对");
                    transaction.hide(luckyFragment);
                    transaction.show(partnerFragment);
                    transaction.hide(meFragment);
                    transaction.hide(starFragment);
                    transaction.commit();
                    break;
                case R.id.r2_rb3:
                    titleTv.setText("运势");
                    transaction.show(luckyFragment);
                    transaction.hide(partnerFragment);
                    transaction.hide(meFragment);
                    transaction.hide(starFragment);
                    transaction.commit();
                    break;
                case R.id.r2_rb4:
                    titleTv.setText("我的");
                    transaction.hide(luckyFragment);
                    transaction.hide(partnerFragment);
                    transaction.show(meFragment);
                    transaction.hide(starFragment);
                    transaction.commit();
                    break;
            }
        }
    };

圆形图像

框架地址: https://github.com/hdodenhof/CircleImageView

Gradle

dependencies {
    ...
    implementation 'de.hdodenhof:circleimageview:3.1.0'
}

usage

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:src="@drawable/profile"
    app:civ_border_width="2dp"
    app:civ_border_color="#FF000000"/>

Bundle

bundle: 放置数据的包装对象

放置数据

Bundle bundle = new Bundle();
bundle.putSerializable("info", dao);
starFragment.setArguments(bundle);

获取数据

在fragment的任意位置使用getArguments()即可

Bundle bundle = getArguments();
InfoDao info = (InfoDao) bundle.getSerializable("info");