内容简介:最近开始开发毕业论文所需的app的前台界面写到用户个人界面这里,记录一些坑,由于我边学边写的,可能有些地方会出现错误首先我用
最近开始开发毕业论文所需的app的前台界面
写到用户个人界面这里,记录一些坑,由于我边学边写的,可能有些地方会出现错误
首先我用 TabLayout + ViewPager + Fragment 搭建好了整个应用的主界面框架。每个界面就是一个Fragment,在查阅google和baidu之后,据说这种搭配会造成数据保存上的麻烦,暂且搁置,后面搭建后台时再进行修改
参考CDSN上面的一篇博客 https://blog.csdn.net/asfang/...
然后,在写用户界面的时候决定照着这篇文章去做,用下面两种开源库去加载图片
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'jp.wasabeef:glide-transformations:2.0.1'
用户界面的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--个人信息-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/h_back"
android:layout_width="match_parent"
android:layout_height="200dp" />
<ImageView
android:id="@+id/h_front"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/h_back"
android:layout_marginBottom="20dp">
<ImageView
android:id="@+id/user_line"
android:layout_width="1dp"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_marginStart="15dp"
android:background="@color/colorWhite" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@id/user_line"
android:text="Profile Fragment"
android:textColor="@color/colorWhite"
android:textSize="17sp" />
<TextView
android:id="@+id/user_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_toEndOf="@id/user_line"
android:text="phone_num"
android:textColor="@color/colorWhite"
android:textSize="17sp" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
思路:
- 最外层是一个LinearLayout,orientation为vertical,包含所有的组件
- 一个RelativeLayout包含图片和用户
- 因为最后的效果是后面是虚化的图片,前面是一个圆形的头像,所以在这里先放两个ImageView
- 中间再加入一个RelativeLayout紧贴着圆形头像下方
/**
* 个人信息和设置页卡Fragment
*/
public class ProfileFragment extends Fragment {
private static final String ARG_FROM = "From";
private String mFrom;
private TextView mTextView;
private ImageView blurImageView;
private ImageView avatarImageView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile_fragment, container, false);
blurImageView = (ImageView) view.findViewById(R.id.h_back);
avatarImageView = (ImageView) view.findViewById(R.id.h_front);
Glide.with(getActivity()).load(R.drawable.person_pic).bitmapTransform(new BlurTransformation(getActivity(), 25), new CenterCrop(getActivity())).into(blurImageView);
Glide.with(getActivity()).load(R.drawable.person_pic).bitmapTransform(new CropCircleTransformation(getActivity())).into(avatarImageView);
return view;
}
public static ProfileFragment newInstance(String from) {
Bundle args = new Bundle();
args.putString(ARG_FROM, from);
ProfileFragment fragment = new ProfileFragment();
fragment.setArguments(args);
return fragment;
}
}
这是我第一次写好的初始化界面代码。
因为我是在Fragment中加载的,之前的文章是直接在Activity中写的,所以获取上下文对象原文可以直接用 this 关键字,我这里用了 getActivity() 代替,发现在Glide加载图片的第一行中出现了空指针异常,然后用 getContext() , getActivity().getApplicationContext() , getContext().getApplicationContext() 都不行,因为这四句都一样的效果,使得Fragment能获得绑定的Activity上下文环境
查阅Google,发现重写 onCreate() 和 onDetach() 之后才能保证不会崩溃
public class ProfileFragment extends Fragment {
public Context mContext;
...
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getContext();
}
@Override
public void onDetach() {
super.onDetach();
mContext = null;
}
}
还有另外一种方法能解决 getActivity() 为 null 的情况,参考自 https://blog.csdn.net/wdd1324...
恢复Fragment之前把保存Bundle里面的数据给清除。赶在Activity恢复其之前所绑定的Fragment之前清除所有存储在 savedInstanceState 中的信息。方法如下:
if (savedInstanceState != null) {
savedInstanceState.putParcelable("android:support:fragments", null);
//或者
//String FRAGMENTS_TAG = "Android:support:fragments";
// remove掉保存的Fragment
// savedInstanceState.remove(FRAGMENTS_TAG);
}
super.onCreate(savedInstanceState);
activity中
@Override
protected void onSaveInstanceState(Bundle outState) {
//super.onSaveInstanceState(outState);
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 记录用户状态
- 域渗透——普通用户权限获得DNS记录
- tab$被恶意删除sys用户之外记录
- Python与SQL对比:处理用户记录相邻的时间差
- 万豪5亿客户开房记录泄露追问:用户该怎么办?这里有详细解答
- 3.64亿条中国用户聊天信息泄露到网上,涉及微信支付记录
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。