編輯:關於Android編程
這個是第一種方法,將View轉為對象來加載。下面還會有第二種方法通過Fragment加載視圖。
先上圖
下面放碼過來了
先上資源母乳!2個java文件,5個xml,View1-4位圖片所見的可滑動的四個界面
activity_main.xml
view1-4類似,上一個
MyPagerAdapter.class
繼承PagerAdapter加入兩個方法,後面幾個需要自己加進去
package com.superxingyun.pageradapter; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * Created by 月滿軒尼詩 on 2016/8/16. */ public class MyPagerAdapter extends PagerAdapter{ private ListviewList; private List titleList; MyPagerAdapter(List viewList, List titleList){ this.viewList = viewList; this.titleList = titleList; } /** *返回頁卡的數量 */ @Override public int getCount() { return viewList.size(); } /** *View是否來自對象 */ @Override public boolean isViewFromObject(View view, Object object) { return view == object; } /** *實例化一個頁卡 */ @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } /** *銷毀一個頁卡 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } /** *為頁卡加載標題 */ @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } }
MainActivity.class
看注釋就能看清楚了
package com.superxingyun.pageradapter; import android.graphics.Color; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.PagerTitleStrip; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListviewList; private ViewPager pager; private PagerTabStrip pagerTabStrip; private List titleList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 viewList = new ArrayList<>(); pager = (ViewPager) findViewById(R.id.pager); //將視圖轉化為對象 View view1 = View.inflate(this ,R.layout.view1, null); View view2 = View.inflate(this ,R.layout.view2, null); View view3 = View.inflate(this ,R.layout.view3, null); View view4 = View.inflate(this ,R.layout.view4, null); //將對象添加到數據源 viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); //為ViewPager頁卡設置標題 titleList = new ArrayList (); titleList.add("第一頁"); titleList.add("第二頁"); titleList.add("第三頁"); titleList.add("第四頁"); //為PagerTabStrip設置屬性 pagerTabStrip = (PagerTabStrip) findViewById(R.id.tab); pagerTabStrip.setBackgroundColor(Color.MAGENTA); pagerTabStrip.setTextColor(Color.WHITE); pagerTabStrip.setDrawFullUnderline(false); pagerTabStrip.setTabIndicatorColor(Color.GREEN); //創建PagerAdapter的適配器 MyPagerAdapter myPagerAdapter = new MyPagerAdapter(viewList, titleList); //ViewPager加載適配器 pager.setAdapter(myPagerAdapter); } }
使用Fragment加載:
Fragment1-4:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">
package com.superxingyun.pageradapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by 月滿軒尼詩 on 2016/8/17.
*/
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.view1, container, false);
}
}
MyFragmentPagerAdapter數據源
package com.superxingyun.pageradapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /** * Created by 月滿軒尼詩 on 2016/8/17. */ public class MyFragmentPagerAdapter extends FragmentPagerAdapter{ private ListfragmentList; private List titleList; public MyFragmentPagerAdapter(FragmentManager fm, List fragments, List titleList) { super(fm); this.fragmentList = fragments; this.titleList = titleList; } @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } }
MainActivity:
package com.superxingyun.pageradapter; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.PagerTabStrip; import android.support.v4.view.PagerTitleStrip; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.util.ArrayList; import java.util.List; public class MainActivity extends FragmentActivity { private ListviewList; private ViewPager pager; private PagerTabStrip pagerTabStrip; private List titleList; private List fragmentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 viewList = new ArrayList<>(); pager = (ViewPager) findViewById(R.id.pager); //將視圖轉化為對象 View view1 = View.inflate(this ,R.layout.view1, null); View view2 = View.inflate(this ,R.layout.view2, null); View view3 = View.inflate(this ,R.layout.view3, null); View view4 = View.inflate(this ,R.layout.view4, null); //將對象添加到數據源 viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); /** * 通過Fragment作為ViewPager的數據源 */ fragmentList = new ArrayList (); fragmentList.add(new Fragment1()); fragmentList.add(new Fragment2()); fragmentList.add(new Fragment3()); fragmentList.add(new Fragment4()); //為ViewPager頁卡設置標題 titleList = new ArrayList (); titleList.add("第一頁"); titleList.add("第二頁"); titleList.add("第三頁"); titleList.add("第四頁"); //為PagerTabStrip設置屬性 pagerTabStrip = (PagerTabStrip) findViewById(R.id.tab); pagerTabStrip.setBackgroundColor(Color.MAGENTA); pagerTabStrip.setTextColor(Color.WHITE); pagerTabStrip.setDrawFullUnderline(false); pagerTabStrip.setTabIndicatorColor(Color.GREEN); //創建PagerAdapter的適配器 MyPagerAdapter myPagerAdapter = new MyPagerAdapter(viewList, titleList); //ViewPager加載適配器 //pager.setAdapter(myPagerAdapter); MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList, titleList); pager.setAdapter(myFragmentPagerAdapter); } }
Material Design引入了深度的UI元素。深入幫助用戶了解每個元素的相對重要性,並把注意力集中到手頭上正在做的事情。 一個視圖(控件),由Z屬性表
Android4.4新特性,系統狀態欄一體化。 實現的步驟主要有以下幾點: 1.android4.4 以上版本 2.設置app全屏: 方法:在AndroidManifes
這個首先感謝魚C論壇的cbs大神,我是看了他的視頻。自己再動手破解他給出這三個小程序。真心這樣無私把技術分享給大家的人真的不多。再次感謝他。這裡我就我自己破解的三個小程序
android中布局一般都有兩種方式,一種xml聲明,另外一種則是程序聲明: xml: