本文已参与「新人创作礼」活动,一起开启掘金创作之路
一、实验目的****
-
理解Android界面的2种编程方式;
-
掌握Android界面中常用布局的用法;
-
掌握Android界面中常用控件的用法;
-
学习如何通过界面编程实现具体的需求。
二、实验内容****
-
根据自己的选题及功能,选择使用特定的界面布局实现首页及功能页;
-
根据自己的选题及功能,选择使用特定的界面控件实现首页及功能页。
三、实验步骤与结果****
(1)在布局页面控件前,先对需要用到的控件一个一个列出来,并规划大概页面布局,常用的布局有线性布局LinerLayout和RelativeLayout布局,常用的控件有TextView,EditText,ImageView,Button等几个常用控件。
(2)通过在一个主Activity里面布置xml为fragment的布局,并在布局完fragment后接着引用一个bottom自定义布局,用来布局下面的导航按钮,设立4个水平分布的imageButton,每张图片上面有TextView对图片进行解释,点击每个图片即可进入不同的fragment页面中,在分别新建4个java文件来对4个fragment页面进行逻辑布局,4个xml文件对4个fragment页面进行页面布局,至此实现导航栏。
(3)代码展示****
主操作页面MainActivity
package Fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import com.example.netcloudsharing.R;
import com.example.netcloudsharing.tool.BaseTool;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentHome fragmentHome;
private FragmentCommunity fragmentCommunity;
private FragmentMessage fragmentMessage;
private FragmentMy fragmentMy;
private ImageButton mImg1;
private ImageButton mImg2;
private ImageButton mImg3;
private ImageButton mImg4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();//初始化控件
initEvent();//初始化事件
selectTab(0);
}
/**
* 初始化事件
*/
private void initEvent(){
mImg1.setOnClickListener(this);
mImg2.setOnClickListener(this);
mImg3.setOnClickListener(this);
mImg4.setOnClickListener(this);
}
/**
* 初始化控件
*/
private void initView(){
//初始化ImageButton布局文件
mImg1 = findViewById(R.id.ib_tab_home);
mImg2 = findViewById(R.id.ib_tab_community);
mImg3 = findViewById(R.id.ib_tab_message);
mImg4 = findViewById(R.id.ib_tab_my);
}
//处理点击事件
@Override
public void onClick(View v) {
resetImages();
switch (v.getId()){
case R.id.ib_tab_home:
selectTab(0);
break;
case R.id.ib_tab_community:
selectTab(1);
break;
case R.id.ib_tab_message:
selectTab(2);break;
case R.id.ib_tab_my:
selectTab(3);
break;
}
}
//进行选中Tab的处理
private void selectTab(int i){
//获取FragmentManager对象
FragmentManager manager = getSupportFragmentManager();
//获取FragmentTransaction对象
FragmentTransaction transaction = manager.beginTransaction();
//先隐藏好所有的Fragment
hideFragment(transaction);
switch (i){
//将选中的Tab实例化并关联起来
case 0:
mImg1.setImageResource(R.drawable.select_home);
if(fragmentHome == null){
fragmentHome = new FragmentHome();
transaction.add(R.id.fl_content,fragmentHome);
}else{
transaction.show(fragmentHome);
}
break;
case 1:
mImg2.setImageResource(R.drawable.select_community);
if(fragmentCommunity == null){
fragmentCommunity = new FragmentCommunity();
transaction.add(R.id.fl_content,fragmentCommunity);
}else{
transaction.show(fragmentCommunity);
}
break;
case 2:
mImg3.setImageResource(R.drawable.select_message);
if(fragmentMessage == null){
fragmentMessage = new FragmentMessage();
transaction.add(R.id.fl_content,fragmentMessage);
}else{
transaction.show(fragmentMessage);
}
break;
case 3:
mImg4.setImageResource(R.drawable.select_my);
if(fragmentMy == null){
fragmentMy = new FragmentMy();
transaction.add(R.id.fl_content,fragmentMy);
}else{
transaction.show(fragmentMy);
}
break;
}
//提交事务
transaction.commit();
}
//将Fragment进行隐藏
private void hideFragment(FragmentTransaction transaction){
if(fragmentHome!=null){
transaction.hide(fragmentHome);
}
if(fragmentCommunity!=null){
transaction.hide(fragmentCommunity);
}
if(fragmentMessage!=null){
transaction.hide(fragmentMessage);
}
if(fragmentMy!=null){
transaction.hide(fragmentMy);
}
}
//重置导航图片
private void resetImages(){
mImg1.setImageResource(R.drawable.unselect_home);
mImg2.setImageResource(R.drawable.unselect_community);
mImg3.setImageResource(R.drawable.unselect_message);
mImg4.setImageResource(R.drawable.unselect_my);
}
}
XML 布局文件 activity.main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="Fragment.MainActivity"
android:orientation="vertical"
android:background="@mipmap/bg2">
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<include layout="@layout/bottom"/>
</LinearLayout>
四个 Fragment 文件,分别为 4 个碎片化界面(四个文件在没实现功能前,均一样)****
package Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.netcloudsharing.R;
public class FragmentMy extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my,container,false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
四、实验体会****
遇到几个警告问题,都已解决
(1 )Accessibility Missing contentDescription attribute on image ?
adt 16及以上版本中的lint支持会引发此警告。
contentDescription定义简要描述视图内容的文本。此属性主要用于可访问性。由于某些视图没有文本表示,因此可以使用该属性来提供文本表示。
像ImageViews和ImageButtons这样的小部件,应该使用contentDescription属性来指定小部件的文本描述,这样屏幕阅读器、文本到语音和其他辅助工具就可以充分描述用户界面。
(2 )Consider adding android:paddingStart=“25dp” to better support right-to-left layouts Error
1.原因分析:
android:paddingstart是新推出的标签在Android 4.2起使用。
现在的RTL级意味着右到左布局被使用在RTL语言如阿拉伯语中。
因此,要开发布局,你可以paddingstart作为填充左侧添加相同的值。所以,当Android渲染RTL布局将考虑paddingstart。
如果你的目标是你的应用程序的Android 4.2(应用程序的targetSdkVersion或minSdkVersion是17或更高),那么你应该使用“开始”和“结束”,而 不是“左”和“右”。例如,android :paddingleft应该成为android:paddingstart。
如果你想让你的应用比Android 4.2版本以前的工作(应用程序的targetSdkVersion或minSdkVersion是16或更少),那么你应该添加“开始”和“除 了“左”和“右”。例如,你可以使用Android的paddingleft和android:paddingstart。
2.解决方法:
android:paddingLeft="25dp"
android:paddingStart="25dp"
android:paddingRight="0dp"
android:paddingEnd="0dp"
3.Missing accessibility label: where minSdk < 17, you should provide an android:hint
缺少可访问性标签:如果minSdk<17,则应提供android:hint