在ScrollView当中只能有一个子控件
布局加载器的获取
两种方式
1 2
| LayoutInflater layoutInflater = LayoutInflater.from(this); LayoutInflater inflater = getLayoutInflater();
|
将view对象添加到listview的头布局
1 2
| ListView lv; lv.addHeaderView(headerView);
|
示例:设置头布局和尾部局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| private void addHeaderAndFooterView() {
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()); 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对应的包头一致
1 2 3 4 5 6 7 8 9 10 11
|
starFragment = new StarFragment();
manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.r2_center, starFragment);
transaction.commit();
|
fragment示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 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 {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_star, container, false); return view; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| 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
1 2 3 4
| dependencies { ... implementation 'de.hdodenhof:circleimageview:3.1.0' }
|
usage
1 2 3 4 5 6 7 8
| <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: 放置数据的包装对象
放置数据
1 2 3
| Bundle bundle = new Bundle(); bundle.putSerializable("info", dao); starFragment.setArguments(bundle);
|
获取数据
在fragment的任意位置使用getArguments()
即可
1 2
| Bundle bundle = getArguments(); InfoDao info = (InfoDao) bundle.getSerializable("info");
|