Android 用户个人中心开发记录(一)

栏目: Android · 发布时间: 5年前

内容简介:最近开始开发毕业论文所需的app的前台界面写到用户个人界面这里,记录一些坑,由于我边学边写的,可能有些地方会出现错误首先我用

最近开始开发毕业论文所需的app的前台界面

写到用户个人界面这里,记录一些坑,由于我边学边写的,可能有些地方会出现错误

首先我用 TabLayout + ViewPager + Fragment 搭建好了整个应用的主界面框架。每个界面就是一个Fragment,在查阅google和baidu之后,据说这种搭配会造成数据保存上的麻烦,暂且搁置,后面搭建后台时再进行修改

Android 用户个人中心开发记录(一)

参考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);
    }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

奔腾年代

奔腾年代

郭万盛 / 中信出版集团 / 2018-9-1 / 68.00

1994年4月20日,一条64K国际专线将中关村地区教育与科研示范网络与互联网连在了一起。中国,成为第77个全功能接入互联网的国家。 1995年,中国互联网正式开始商业化应用。浪潮开始! 这是一个波澜壮阔的年代,带给我们翻天覆地的变化。中国互联网25年发展史的全景展示、忠实梳理和记录。 在更宏观的角度审视互联网与中国的关系,人们将会发现,互联网革命给中国带来了重新崛起的时代机遇。......一起来看看 《奔腾年代》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具