編輯:關於Android編程
Android高級控件——ViewPager、GridView、popwindow、SlideMenu(下)
android:screenOrientation="locked"鎖屏
android:screenOrientation="landscape"橫屏鎖定
//隱藏標題欄
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_zyf_text);
//隱藏ActionBar
getSupportActionBar().hide();
ViewPager滾動 + 滑動3頁監聽
ViewPager
橫向滑動的分頁組件
應用十分廣泛的組件之一,如實現不同頻道的左右滑動顯示
同ListView一樣屬於適配器控件,ViewPager專屬適配器PagerAdapter
package com.example.jreduch7292; import android.content.Intent;import android.os.Bundle;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.ImageView;import java.util.ArrayList;import java.util.List;public class ZyfTextActivity extends AppCompatActivity { private ViewPager vp; private List list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); //隱藏標題欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_zyf_text); //隱藏ActionBar getSupportActionBar().hide(); vp=(ViewPager)findViewById(R.id.vp); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if(position==3){ Intent intent=new Intent(ZyfTextActivity.this,GridViewActivity.class); startActivity(intent); finish(); } } @Override public void onPageScrollStateChanged(int state) { } }); // (Context context,ListList) list=new ArrayList<>(); ImageView img=new ImageView(this); img.setImageResource(R.mipmap.a); list.add(img); img=new ImageView(this); img.setImageResource(R.mipmap.b); list.add(img); img=new ImageView(this); img.setImageResource(R.mipmap.c); list.add(img); img=new ImageView(this); list.add(img); vp.setAdapter(new MyViewPagerAdapter(list)); } public class MyViewPagerAdapter extends PagerAdapter { private List list; public MyViewPagerAdapter(Listlist){ this.list=list; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position)); } }}
使用GridView制作分享界面
package com.example.jreduch7292; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ZyfPopActivity extends AppCompatActivity { private Button bt1; private Button bb; private GridView gv; private PopupWindow pw; private View popView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zyf_pop); bt1=(Button) findViewById(R.id.bt1); popView= getLayoutInflater().inflate(R.layout.activity_grid_view,null); gv=(GridView)popView.findViewById(R.id.gv) ; bb=(Button)popView.findViewById(R.id.bb); List list=new ArrayList(); Map map=new HashMap(); map.put("img",R.mipmap.a); map.put("name","字母A"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.b); map.put("name","字母B"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.c); map.put("name","字母C"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.d); map.put("name","字母D"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.e); map.put("name","字母E"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.f); map.put("name","字母F"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.g); map.put("name","字母G"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.h); map.put("name","字母H"); list.add(map); SimpleAdapter sa=new SimpleAdapter(this,list,R.layout.grid_layout, new String[]{"img","name"}, new int[]{R.id.iv,R.id.tv}); gv.setAdapter(sa); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pw=getPopWindow(popView); //設置VIEW隱藏 bt1.setVisibility(View.GONE); } }); bb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pw.dismiss(); // WindowManager.LayoutParams ll=getWindow().getAttributes(); // ll.alpha=1f; // getWindow().setAttributes(ll); //設置VIEW可見 // bt1.setVisibility(View.VISIBLE); } }); } public PopupWindow getPopWindow(View view){ PopupWindow popupWindow=new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); popupWindow.setOutsideTouchable(false); popupWindow.setAnimationStyle(R.style.popStyle); //設置背景透明度 WindowManager.LayoutParams ll=getWindow().getAttributes(); ll.alpha=0.6f; getWindow().setAttributes(ll); //---------------------------------- popupWindow.setBackgroundDrawable(new ColorDrawable());//返回 popupWindow.showAtLocation(bt1, Gravity.BOTTOM,0,0); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams ll=getWindow().getAttributes(); ll.alpha=1f; getWindow().setAttributes(ll); bt1.setVisibility(View.VISIBLE); } }); return popupWindow; } }
SlideMenu側邊欄
slidingMenu = new SlidingMenu(this); //new一個側邊欄
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT); slidingMenu.setMenu(menuView_L);
//將布局加載到側邊欄 slidingMenu.setSecondaryMenu(v1); slidingMenu.setBehindWidth(300);
//側邊欄出來的寬度 slidingMenu.setBehindOffset(380);
//剩余部分的寬度 slidingMenu.setFadeDegree(0.35f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//側邊欄執行開始
SlidingMenu常用屬性介紹
mSlidingMenu.setMode(SlidingMenu.LEFT);//設置左滑菜單
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//設置滑動的屏幕范圍,該設置為全屏區域都可以滑動
mSlidingMenu.setShadowDrawable(R.drawable.shadow);//設置陰影圖片
mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);//設置陰影圖片的寬度
mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu劃出時主頁面顯示的剩余寬度
mSlidingMenu.setBehindWidth(400);//設置SlidingMenu菜單的寬度
mSlidingMenu.setFadeDegree(0.35f);//SlidingMenu滑動時的漸變程度
mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//使SlidingMenu附加在Activity上
mSlidingMenu.setMenu(R.layout.menu_layout);//設置menu的布局文件
mSlidingMenu.toggle();//動態判斷自動關閉或開啟SlidingMenu
mSlidingMenu.showMenu();//顯示SlidingMenu
mSlidingMenu.showContent();//顯示內容
mSlidingMenu.setOnOpenListener(onOpenListener);//監聽SlidingMenu打開
關於關閉menu有兩個監聽,簡單的來說,對於menu close事件,一個是when,一個是after
mSlidingMenu.OnClosedListener(OnClosedListener);//監聽SlidingMenu關閉時事件
mSlidingMenu.OnClosedListener(OnClosedListener);//監聽SlidingMenu關閉後事件
mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);//設置左右都可以劃出SlidingMenu菜單
mSlidingMenu.setSecondaryMenu(R.layout.frame_menu);//設置右側菜單的布局文件
mSlidingMenu.setSecondaryShadowDrawable(R.drawable.shadowright);//右側菜單的陰影圖片
package com.example.jreduch7292; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; public class SlidingMenuActivity extends AppCompatActivity { private ImageView iv1; private ImageView iv2; private ImageView iv3; private LinearLayout ll1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // getSupportActionBar().hide(); setContentView(R.layout.activity_sliding_menu); SlidingMenu slidingMenu=new SlidingMenu(this);//new 一個側邊欄 // View v=getLayoutInflater().inflate(R.layout.activity_grid_view,null); View v=getLayoutInflater().inflate(R.layout.activity_duu,null); View v1=getLayoutInflater().inflate(R.layout.activity_duu,null); iv1=(ImageView)v1.findViewById(R.id.iv1); iv2=(ImageView)v1.findViewById(R.id.iv2); iv3=(ImageView)v1.findViewById(R.id.iv3); ll1=(LinearLayout)v1.findViewById(R.id.ll1); //設置屏幕滑動范圍 slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); //設置屏幕左側劃出布局 // slidingMenu.setMenu(v);//將布局加載到側邊欄 //設置劃出模式 支持 左右都能劃 ; slidingMenu.setMode(SlidingMenu.LEFT_RIGHT); slidingMenu.setMenu(v1); slidingMenu.setSecondaryMenu(v); // slidingMenu.setBehindWidth(300);//側邊欄出來的寬度 slidingMenu.setBehindOffset(380);//剩余部分的寬度 slidingMenu.setFadeDegree(0.35f); //附加到Activity // slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);//側邊欄執行開始 slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_WINDOW);//推bar iv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("====","error圖片1"); } }); iv2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("====","error圖片2"); } }); iv3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("====","error圖片3"); } }); ll1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("====","error搜索"); } }); } }
在上一篇文章中,我們詳細分析了android是如何解析藍牙反饋上來的搜索到的設備信息,本文將會繼續分析這些信息到了上層之後是如何處理。 8、inquiry result引
Android-PullRefreshLayout簡介:基於SwipeRefreshLayout,通吃所有的AbsListView、RecyclerViewAndroid
我們已經了解了android觸摸事件傳遞機制,接著我們再來研究一下與觸摸事件傳遞相關的幾個比較重要的類,比如MotionEvent。我們今天就來詳細說明一下這個類的各方面
今天有人問我,android系統不同分辨率,不同大小的手機,字體大小怎麼去適應呢?其實字體的適應和圖片的適應是一個道理的。 一、 原理如下: 假設需要適應320x240,
android的selector對於android開發者而言再熟悉不過了