Skip to content

Commit

Permalink
fix dialog 内部view的context强转异常
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Dec 4, 2015
1 parent d9ad7cb commit ff2d74b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ dependencies {

```
dependencies {
compile 'com.zhy:autolayout:1.2.0'
compile 'com.zhy:autolayout:1.2.1'
}
```

Expand Down
2 changes: 1 addition & 1 deletion autolayout/autolayout.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":autolayout" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.zhy" external.system.module.version="1.2.0" type="JAVA_MODULE" version="4">
<module external.linked.project.id=":autolayout" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.zhy" external.system.module.version="1.2.1" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down
2 changes: 1 addition & 1 deletion autolayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.2.0"
version = "1.2.1"

android {
compileSdkVersion 23
Expand Down
32 changes: 16 additions & 16 deletions autolayout/src/main/java/com/zhy/autolayout/AutoLayoutConifg.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.content.res.TypedArray;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

Expand All @@ -17,14 +16,15 @@
*/
public class AutoLayoutConifg
{

private boolean mStatusBarAvailable;
private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";


DisplayMetrics outMetrics = new DisplayMetrics();

private int mAvailableWidth;
private int mAvailaleHegiht;
private int mAvailableHeight;


private static final String KEY_DESIGN_WIDTH = "design_width";
Expand All @@ -39,9 +39,9 @@ public int getAvailableWidth()
return mAvailableWidth;
}

public int getAvailaleHeight()
public int getAvailableHeight()
{
return mAvailaleHegiht;
return mAvailableHeight;
}

public int getDesignWidth()
Expand All @@ -54,33 +54,35 @@ public int getDesignHeight()
return mDesignHeight;
}

public void auto(Activity activity)
public void auto(Context context)
{
auto(activity, true);
auto(context, true);
}


public void auto(Activity activity, boolean ignoreStatusBar)
public void auto(Context context, boolean ignoreStatusBar)
{
getMetaData(activity);
getMetaData(context);

WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(outMetrics);
mAvailableWidth = outMetrics.widthPixels;
mAvailaleHegiht = outMetrics.heightPixels;
mAvailaleHegiht -= getStatusHeight(activity.getResources());
mAvailableHeight = outMetrics.heightPixels;
mAvailableHeight -= getStatusHeight(context.getResources());

if (ignoreStatusBar)
return;

checkStatusBar(activity);
if (!(context instanceof Activity))
return;
checkStatusBar((Activity) context);

if (mStatusBarAvailable)
{
mAvailaleHegiht += getStatusHeight(activity.getResources());
mAvailableHeight += getStatusHeight(context.getResources());
}

L.e("mAvailableWidth =" + mAvailableWidth + " , mAvailaleHegiht = " + mAvailaleHegiht);
L.e("mAvailableWidth =" + mAvailableWidth + " , mAvailableHeight = " + mAvailableHeight);
}

private void getMetaData(Context context)
Expand Down Expand Up @@ -109,7 +111,6 @@ private void getMetaData(Context context)
private void checkStatusBar(Activity activity)
{
Window win = activity.getWindow();
ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
Expand Down Expand Up @@ -150,5 +151,4 @@ private int getStatusHeight(Resources res)




}
27 changes: 19 additions & 8 deletions autolayout/src/main/java/com/zhy/autolayout/AutoLayoutHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.zhy.autolayout;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
Expand Down Expand Up @@ -64,22 +63,34 @@ public class AutoLayoutHelper
private static final String VAL_WRAP_CONTENT = "-2";
private static final String VAL_MATCH_PARENT = "-1";


/**
* TODO move to other place
*/
private AutoLayoutConifg mAutoLayout;
private static AutoLayoutConifg mAutoLayoutConifg;

public AutoLayoutHelper(ViewGroup host)
{
mHost = host;
mAutoLayout = new AutoLayoutConifg();

if (mAutoLayoutConifg == null)
{
initAutoLayoutConfig(host);
}


}

private void initAutoLayoutConfig(ViewGroup host)
{
mAutoLayoutConifg = new AutoLayoutConifg();

boolean ignoreStatusBar = true;
if (host.getContext() instanceof UseStatusBar)
{
ignoreStatusBar = false;
}
mAutoLayout.auto((Activity) host.getContext(), ignoreStatusBar);
mAutoLayoutConifg.auto(host.getContext(), ignoreStatusBar);
}


Expand Down Expand Up @@ -193,23 +204,23 @@ private void supportPadding(View view, AutoLayoutInfo info)

private int getAvailableWidth()
{
return mAutoLayout.getAvailableWidth();
return mAutoLayoutConifg.getAvailableWidth();
}

private int getAvailableHeight()
{
return mAutoLayout.getAvailaleHeight();
return mAutoLayoutConifg.getAvailableHeight();

}

private int getDesignWidth()
{
return mAutoLayout.getDesignWidth();
return mAutoLayoutConifg.getDesignWidth();
}

private int getDesignHeight()
{
return mAutoLayout.getDesignHeight();
return mAutoLayoutConifg.getDesignHeight();
}


Expand Down

0 comments on commit ff2d74b

Please sign in to comment.