在fragment里显示版本信息
时间: 2024-05-10 09:18:36 浏览: 10
可以在fragment的布局文件中添加一个TextView来显示版本信息,并在fragment的代码中获取当前应用的版本信息并设置给这个TextView。
1. 在fragment的布局文件中添加一个TextView:
```
<TextView
android:id="@+id/version_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
```
2. 在fragment的代码中获取当前应用的版本信息并设置给这个TextView:
```
public class MyFragment extends Fragment {
private TextView mVersionTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
mVersionTextView = view.findViewById(R.id.version_text_view);
String versionName = getVersionName();
mVersionTextView.setText("版本号:" + versionName);
return view;
}
private String getVersionName() {
try {
PackageInfo packageInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0);
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return "";
}
}
```
这样就可以在fragment里显示当前应用的版本信息了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)