及Back鍵要如何也有取消的功能
花了不少時間找,應該以下這個連結能夠解決問題
http://blog.csdn.net/woshicaixianfeng/article/details/7075066
及量測 measure 時,會有平版發生Exception
只好把我改過的PopupWindow改成如下
LayoutInflater layoutInflater =(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); View popupView = layoutInflater.inflate(R.layout.popup_setting, null); popupWindow = new PopupWindow(popupView,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); RelativeLayout lay_popwin = (RelativeLayout) popupView.findViewById(R.id.lay_popwin); lay_popwin.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) popupWindow.dismiss(); return false; } }); int xOffset = 0; try { popupView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); xOffset = -(popupView.getMeasuredWidth() - popupView.getWidth()); }catch (Exception ex) { DisplayMetrics displayMetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); //平版measure出錯,直接處理xOffset //依比例調整xOffset int dp = 150;//popupWindow 為 150 dp xOffset = -(int)(dp*displayMetrics.scaledDensity); ex.printStackTrace(); } //解決點外無法取消Popup Window問題popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 使其聚集popupWindow.setFocusable(true); // 设置允许在外点击消失popupWindow.setOutsideTouchable(true); //刷新状态(必须刷新否则无效)popupWindow.update(); popupWindow.showAsDropDown(btn_setting, xOffset + 70, -10);
FrameLayout layout = (FrameLayout) findViewById(R.id.layout); ViewGroup.LayoutParams lp =layout.getLayoutParams(); DisplayMetrics displayMetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); String px = displayMetrics.widthPixels + " x " + displayMetrics.heightPixels; String dp = displayMetrics.xdpi + " x " + displayMetrics.ydpi; String density = "densityDpi = " + displayMetrics.densityDpi + ", density=" + displayMetrics.density + ", scaledDensity = " + displayMetrics.scaledDensity; Log.e("test", "px = " + px + ", dp = " + dp + ",density " + density); Log.e("test", "framelayout height = " + lp.height); Log.e("test", "framelayout width = " + lp.width); lp.height = displayMetrics.widthPixels/3;
參考網址:
http://saminjava.blogspot.tw/2013/09/android.html
http://ikevin.tw/?cat=9
<!--ViewPager Radio Button --><style name="viewpagerindicator_circle"> <item name="android:layout_width">11dp</item> <item name="android:layout_height">11dp</item> <item name="android:button">@drawable/view_pager_circle_background</item> <item name="android:editable">false</item> <item name="android:layout_marginLeft">2dp</item> <item name="android:layout_marginRight">2dp</item> </style>
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Larger blue circle in back --> <item> <shape android:shape="oval"> <solid android:color="#0fff"/> <size android:width="9.5dp" android:height="9.5dp"/> </shape> </item> <!-- Smaller red circle in front --> <item> <shape android:shape="oval"> <!-- transparent stroke = larger_circle_size - smaller_circle_size --> <stroke android:color="@android:color/transparent" android:width="5dp"/> <solid android:color="#cfff"/> <size android:width="7.5dp" android:height="7.5dp"/> </shape> </item> </layer-list>
/** * 取得Radio Button Circle Indicator * @return */public RadioButton getRadioButtons() { float radio_dps = 9.5f; float margin_dps = 2f; final float scale = getResources().getDisplayMetrics().density; int radioSize = (int) (radio_dps * scale + 0.5f); int margin = (int) (margin_dps * scale + 0.5f); RadioButton radio = (RadioButton)getLayoutInflater().inflate(R.layout.item_view_pager_circle, null); radio.setHeight(radioSize); radio.setWidth(radioSize); radio.setButtonDrawable(R.drawable.view_pager_circle_background); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1); // , 1是可選寫的 lp.setMargins(margin, 0, margin, 0); lp.gravity = Gravity.CENTER; radio.setLayoutParams(lp); radio.setEnabled(false); return radio; }
radioGroup.addView(getRadioButtons());
RadioButton radio = (RadioButton) radioGroup.getChildAt(0); radio.setChecked(true);
// 启动banner自动轮播public void bannerStartPlay() { if (mBannerTimer != null) { if (mBannerTimerTask != null) mBannerTimerTask.cancel(); mBannerTimerTask = new BannerTimerTask(); mBannerTimer.schedule(mBannerTimerTask, 5000, 5000);// 5秒后执行,每隔5秒执行一次 } } // 暂停banner自动轮播public void bannerStopPlay() { if (mBannerTimerTask != null) mBannerTimerTask.cancel(); } class BannerTimerTask extends TimerTask { @Override public void run() { Message msg = new Message(); if (pagerAdapter.getCount() <= 1) return; int currentIndex = viewPager.getCurrentItem(); if (currentIndex == pagerAdapter.getCount() - 1) msg.what = 0; else msg.what = currentIndex + 1; mTimerHandler.sendMessage(msg); } }
mBannerTimer = new Timer(); mTimerHandler = new Handler() { public void handleMessage(Message msg) { viewPager.setCurrentItem(msg.what); super.handleMessage(msg); } };
GridView gv = findViewById(R.id.my_grid_view);
gv.setBackgroundColor(Color.WHITE);
gv.setVerticalSpacing(1);
gv.setHorizontalSpacing(1);
及item也設背景,就可以完成格線。<View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/white"/>
http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en
取得以下資訊
In Android 6.0 Marshmallow, application will not be granted any permission at installation time. Instead, application has to ask user for a permission one-by-one at runtime.
Anyway this new Runtime Permission will work like described only when we set the application'stargetSdkVersion to 23 which mean it is declared that application has already been tested on API Level 23. And this feature will work only on Android 6.0 Marshmallow. The same app will run with same old behavior on pre-Marshmallow device.
很不湊巧如果使用的是targetSdkVersion 23的話,
過去的權限方式,將會出問題。
加AndroidMainfest.xml不一定能解決問題。
因為我剛好使用的是開發模式,直接Builder進手機,
然後就直接出問題。
可能沒有直接授權的關係。
需要到「設定」→「應用程式」→權限,變更權限後,
這種問題就能夠被解決。(在開發方面的問題)
就不會在開發時,一直卡關在「java.lang.SecurityException」這個問題上
package com.example;
public class ExpandableHeightGridView extends GridView
{
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
<com.example.ExpandableHeightGridView
android:id="@+id/myId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="2dp"
android:isScrollContainer="false"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="20dp" />
最開始的時候要設定mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId);
mAppsGrid.setExpanded(true);
scrollView = (ScrollView) findViewById(R.id.scrollView); scrollView.smoothScrollTo(0, 0);