封面图版权声明:未经博主允许不得转载
一:简介
【达叔有道】软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family !
二:项目说明
项目还好,难度不大,接下来说明一下如何制作广告显示已经一些UI布局等。
三:项目代码说明
一般地,点击一款APP,会有一个页面图展示效果,几秒后跳转到主界面。
SplashActivity.java
public class SplashActivity extends AppCompatActivity{
protected Handler mHandler=new Handler();//首先创建Handler对象
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//对象.postDelayed实现该方法
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
//设置时间并跳转
startActivity(new Intent(SplashActivity.this,MainActivity.class));
}
},2000);
}
}
创建主界面等,创建Fragment布局,如同微信下方点击效果。分别创建fragment.xml布局,fragment.java等。
public class MainFragment extends Fragment {
//创建完,继承Fragment,extends Fragment
//导入import android.support.v4.app.Fragment;
//点击ctrl+o,导入方法,不用自己写onCreateView
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//返回自己渲染的布局
return inflater.inflate(R.layout.fragment_main,container,false);
}
}
然后再主方法中添加动态生成Fragment,MainActivity.java中添加Fragment。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
}
//添加点击效果
protected MainFragment mMainFragment=new MainFragment();//首页
//创建对象
this.getSupportFragmentManager()
.beginTransaction()
.add(R.id.container_content,mMainFragment)
.add(R.id.container_content,mMainoneFragmenr)//添加
.hide(mMainoneFragmenr)//隐藏
.add(R.id.container_content,mMaintwoFragment)//添加
.hide(mMaintwoFragment)//隐藏
//事物添加
//默认显示首页 其他页面隐藏
//提交
.commit();
//获取管理类
这样就可以显示了,模仿微信我的界面布局,用点击我的按钮,加载fragment布局。滚动列表展示功能等。
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/list_my_menu"
android:layout_marginLeft="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/txt_my_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的服务"
android:textSize="24sp"
android:textColor="#000000"
android:paddingTop="18dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#D8DDE1"
android:layout_below="@+id/txt_my_menu"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"></View>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/txt_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的设置"
android:textSize="24sp"
android:textColor="#000000"
android:paddingTop="18dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
我的界面fragment.java
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_me,container,false);
}
添加广告
在布局文件中添加ViewPager
<android.support.v4.view.ViewPager
android:id="@+id/vpager_main_header_ad"
android:layout_width="match_parent"
android:layout_height="120dp">
</android.support.v4.view.ViewPager>
MainFragment.java
protected ViewPager mVPagerHeaderAd;//广告头
//onActivityCreated
mVPagerHeaderAd= (ViewPager) getView().findViewById(R.id.vpager_main_header_ad);
//添加适配器
MainHeaderAdAdapter adapter=new MainHeaderAdAdapter(getActivity(), DataUtil.getHeaderAddInfo(getActivity(),icons));
mVPagerHeaderAd.setAdapter(adapter);
//创建一个适配器文件
public class MainHeaderAdAdapter extends PagerAdapter{
}
//继承extends PagerAdapter
//再写两个方法
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(images.get(position));
return images.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
protected Context context;
protected List<ImageView> images;
public MainHeaderAdAdapter(Context context, List<ImageView> images){
this.context=context;
this.images=images;
}
//添加数据图片DataUtil
public static List<ImageView> getHeaderAddInfo(Context context, int icons[]){
List<ImageView> datas=new ArrayList<>();
for (int i = 0; i <icons.length ; i++) {
ImageView icon=new ImageView(context);
icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
icon.setImageResource(icons[i]);
datas.add(icon);
}
return datas;
}
排布
protected RecyclerView mRecycleViewMenu;//主菜单
mRecycleViewMenu= (RecyclerView) getView().findViewById(R.id.recycleview_main_menu);
mRecycleViewMenu.setLayoutManager(new GridLayoutManager(getActivity(),4));
MainMenuAdapter mainMenuAdapter=new MainMenuAdapter(getActivity(),DataUtil.getMainMenus(menuIons,menus));
mRecycleViewMenu.setAdapter(mainMenuAdapter);
//
protected String [] menus;
menus=this.getActivity().getResources().getStringArray(R.array.main_menu);
//adpater
extends RecyclerView.Adapter<MainMenuAdapter.MainMenuViewholder>
//onBindViewHolder
// ViewHolder
class MainMenuViewholder extends RecyclerView.ViewHolder{
public ImageView mImgMenuIcon;
public TextView mTextMenuName;
public MainMenuViewholder(@NonNull View itemView) {
super(itemView);
mImgMenuIcon=itemView.findViewById(R.id.img_menu_icon);
mTextMenuName=itemView.findViewById(R.id.txt_menu_name);
}
}
//
public int icon;
public String menuName;
public Menu(int icon,String menuName){
this.icon=icon;
this.menuName=menuName;
}
build.gradler
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.android.support:recyclerview-v7:26.1.0'
}
//compile 'com.android.support:recyclerview-v7:26.1.0'
//implementation 'com.android.support:appcompat-v7:26.1.0'
总结
统一控件版本,# TravelDemo
编辑 :达叔
定位:分享 Android&Java 知识点
我是简书的达叔小生。
网友评论