下面列出了android.util.TypedValue#complexToDimensionPixelSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setupSwipeContainer(){
//disable it for search fragment
searchSwipeContainer.setEnabled(false);
searchSwipeContainer.setColorSchemeResources(android.R.color.holo_green_dark,
android.R.color.holo_red_dark,
android.R.color.holo_blue_dark,
android.R.color.holo_orange_dark);
TypedValue tv = new TypedValue();
if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
getContext().getResources().getDisplayMetrics());
//push it down to the same position as the first item to be loaded
searchSwipeContainer.setProgressViewOffset(false, 0 , 50);
}
}
/**
* Add a margin with a combined height of the ActionBar and Status bar to the top of a given View contained in a FrameLayout
* @param c
* @param v
*/
public static void addActionBarAndStatusBarMargin(Context c, View v, FrameLayout.LayoutParams layoutParams) {
int mActionBarHeight = 0, mStatusBarHeight = 0;
TypedValue tv = new TypedValue();
if (c.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, c.getResources().getDisplayMetrics());
else
mActionBarHeight = 0; // No ActionBar style (pre-Honeycomb or ActionBar not in theme)
if (hasKitKat())
mStatusBarHeight = convertDpToPixels(c, 25);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.setMargins(0, mActionBarHeight + mStatusBarHeight, 0, 0);
params.gravity = layoutParams.gravity;
v.setLayoutParams(params);
}
@TargetApi(14)
private int getActionBarHeight(Activity activity) {
int result = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
View actionBar = activity.getWindow().findViewById(R.id.action_bar_container);
if (actionBar != null) {
result = actionBar.getMeasuredHeight();
}
if (result == 0) {
TypedValue tv = new TypedValue();
activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
result = TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
}
}
return result;
}
public static int getActionBarHeight(Activity activity) {
int actionBarHeight = 0;
if(activity.getActionBar()!= null){
actionBarHeight = activity.getActionBar().getHeight();
}
if (actionBarHeight != 0)
return actionBarHeight;
final TypedValue tv = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (activity.getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true)){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
Log.e("actionBarHeight==",actionBarHeight + "android.support.v7.appcompat.R.attr.actionBarSize");
}
else if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
Log.e("actionBarHeight==",actionBarHeight + "actionBarHeight is android.R.attr.actionBarSize");
}
}
return actionBarHeight;
}
/**
* 获取actionbar的像素高度,默认使用android官方兼容包做actionbar兼容
*
* @return
*/
public static int getActionBarHeight(Context context) {
int actionBarHeight=0;
if(context instanceof AppCompatActivity &&((AppCompatActivity) context).getSupportActionBar()!=null) {
Log.d("isAppCompatActivity", "==AppCompatActivity");
actionBarHeight = ((AppCompatActivity) context).getSupportActionBar().getHeight();
}else if(context instanceof Activity && ((Activity) context).getActionBar()!=null) {
Log.d("isActivity","==Activity");
actionBarHeight = ((Activity) context).getActionBar().getHeight();
}else if(context instanceof ActivityGroup){
Log.d("ActivityGroup","==ActivityGroup");
if (((ActivityGroup) context).getCurrentActivity() instanceof AppCompatActivity && ((AppCompatActivity) ((ActivityGroup) context).getCurrentActivity()).getSupportActionBar()!=null){
actionBarHeight = ((AppCompatActivity) ((ActivityGroup) context).getCurrentActivity()).getSupportActionBar().getHeight();
}else if (((ActivityGroup) context).getCurrentActivity() instanceof Activity && ((Activity) ((ActivityGroup) context).getCurrentActivity()).getActionBar()!=null){
actionBarHeight = ((Activity) ((ActivityGroup) context).getCurrentActivity()).getActionBar().getHeight();
}
}
if (actionBarHeight != 0)
return actionBarHeight;
final TypedValue tv = new TypedValue();
if(context.getTheme().resolveAttribute( android.support.v7.appcompat.R.attr.actionBarSize, tv, true)){
if (context.getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}else {
if (context.getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
Log.d("actionBarHeight","===="+actionBarHeight);
return actionBarHeight;
}
@TargetApi(14)
private int getActionBarHeight(Context context) {
int result = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
return result;
}
@NonNull
private MoviesAdapter createAdapter() {
TypedValue typedValue = new TypedValue();
getResources().getValue(R.dimen.rows_count, typedValue, true);
float rowsCount = typedValue.getFloat();
int actionBarHeight = getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true)
? TypedValue.complexToDimensionPixelSize(typedValue.data, getResources().getDisplayMetrics())
: 0;
int imageHeight = (int) ((getResources().getDisplayMetrics().heightPixels - actionBarHeight) / rowsCount);
int columns = getResources().getInteger(R.integer.columns_count);
int imageWidth = getResources().getDisplayMetrics().widthPixels / columns;
return new MoviesAdapter(imageHeight, imageWidth, this);
}
public static int getActionBarHeight(Context context) {
if (sActionBarHeight != 0) {
return sActionBarHeight;
}
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, sTypedValue, true);
sActionBarHeight = TypedValue.complexToDimensionPixelSize(sTypedValue.data, context.getResources().getDisplayMetrics());
return sActionBarHeight;
}
@TargetApi(14)
private int getActionBarHeight(Context context) {
int result = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
return result;
}
public static int getToolBarHeight(Context context) {
TypedValue tv = new TypedValue();
return context.getTheme().resolveAttribute(
android.R.attr.actionBarSize,
tv,
true) ? TypedValue.complexToDimensionPixelSize(
tv.data,
context.getResources().getDisplayMetrics()) : 0;
}
/**
* @hide
*/
public void updateDimensions(DisplayMetrics displayMetrics) {
// Converting complex to dp.
minWidth = TypedValue.complexToDimensionPixelSize(minWidth, displayMetrics);
minHeight = TypedValue.complexToDimensionPixelSize(minHeight, displayMetrics);
minResizeWidth = TypedValue.complexToDimensionPixelSize(minResizeWidth, displayMetrics);
minResizeHeight = TypedValue.complexToDimensionPixelSize(minResizeHeight, displayMetrics);
}
public static int getSystemActionBarSize(Context context) {
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
return TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
} else {
return dpToPx(context, 48);
}
}
public static int getActionBarHeight(Context context) {
// int contentTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
// //statusBarHeight是上面状态栏的高度
//// int titleBarHeight = contentTop - statusBarHeight;
// return contentTop;
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
}
return actionBarHeight;
}
/**
* Calculating width based on
* http://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-specs.
*/
private void setOptimalDrawerWidth(View drawerContainer) {
int actionBarSize = 0;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarSize = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
ViewGroup.LayoutParams params = drawerContainer.getLayoutParams();
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int minScreenWidth = min(displayMetrics.widthPixels, displayMetrics.heightPixels);
params.width = min(minScreenWidth - actionBarSize, 5 * actionBarSize);
drawerContainer.requestLayout();
}
public static int getActionBarHeightPixels(final Context pContext) {
final TypedValue typedValue = new TypedValue();
if (pContext != null && pContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true)) {
return TypedValue.complexToDimensionPixelSize(typedValue.data, pContext.getResources().getDisplayMetrics());
}
return 0;
}
/**
* Return the action bar's height.
*
* @return the action bar's height
*/
public static int getActionBarHeight() {
TypedValue tv = new TypedValue();
if (UtilsApp.getApp().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
return TypedValue.complexToDimensionPixelSize(
tv.data, UtilsApp.getApp().getResources().getDisplayMetrics()
);
}
return 0;
}
/**
* 获取 ActionBar 高度
* @return ActionBar 高度
*/
public static int getActionBarHeight() {
TypedValue tv = new TypedValue();
try {
if (DevUtils.getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
return TypedValue.complexToDimensionPixelSize(tv.data, Resources.getSystem().getDisplayMetrics());
}
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getActionBarHeight");
}
return 0;
}
public static int getToolbarHeight(Context context) {
TypedValue typedValue = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true)) {
return TypedValue.complexToDimensionPixelSize(typedValue.data, context.getResources().getDisplayMetrics());
}
return 0;
}
/**
* 获取actionbar的像素高度,默认使用android官方兼容包做actionbar兼容
*
* @return
*/
public static int getActionBarHeight(Context context) {
int actionBarHeight = 0;
if (context instanceof AppCompatActivity && ((AppCompatActivity) context)
.getSupportActionBar() != null) {
Log.d("isAppCompatActivity", "==AppCompatActivity");
actionBarHeight = ((AppCompatActivity) context).getSupportActionBar().getHeight();
} else if (context instanceof Activity && ((Activity) context).getActionBar() != null) {
Log.d("isActivity", "==Activity");
actionBarHeight = ((Activity) context).getActionBar().getHeight();
} else if (context instanceof ActivityGroup) {
Log.d("ActivityGroup", "==ActivityGroup");
if (((ActivityGroup) context).getCurrentActivity() instanceof AppCompatActivity && (
(AppCompatActivity) ((ActivityGroup) context).getCurrentActivity())
.getSupportActionBar() != null) {
actionBarHeight = ((AppCompatActivity) ((ActivityGroup) context)
.getCurrentActivity()).getSupportActionBar().getHeight();
} else if (((ActivityGroup) context).getCurrentActivity() instanceof Activity && (
(Activity) ((ActivityGroup) context).getCurrentActivity()).getActionBar() !=
null) {
actionBarHeight = ((Activity) ((ActivityGroup) context).getCurrentActivity())
.getActionBar().getHeight();
}
}
if (actionBarHeight != 0)
return actionBarHeight;
final TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.support.v7.appcompat.R.attr
.actionBarSize, tv, true)) {
if (context.getTheme().resolveAttribute(android.support.v7.appcompat.R.attr
.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context
.getResources().getDisplayMetrics());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context
.getResources().getDisplayMetrics());
} else {
if (context.getTheme().resolveAttribute(android.support.v7.appcompat.R.attr
.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context
.getResources().getDisplayMetrics());
}
Log.d("actionBarHeight", "====" + actionBarHeight);
return actionBarHeight;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mUiState = new UIState(getApplication());
SharedPreferences pref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
mUiState.loadState(pref);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("");
mNavSpinner = (Spinner) findViewById(R.id.nav_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(
actionBar.getThemedContext(), R.layout.spinner_title,
FragmentIndex.getStrings(this));
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
mNavSpinner.setAdapter(adapter);
mNavSpinner.setOnItemSelectedListener(this);
// Created a scaled-down icon for the Toolbar.
{
TypedValue tv = new TypedValue();
getTheme().resolveAttribute(R.attr.actionBarSize, tv, true);
int height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
// This originally used a scaled-down launcher icon, but I don't feel like figuring
// out how to render R.mipmap.chromadoze_icon correctly.
mToolbarIcon = ContextCompat.getDrawable(this, R.drawable.toolbar_icon);
}
// When this Activity is first created, set up the initial fragment.
// After a save/restore, the framework will drop in the last-used
// fragment automatically.
if (savedInstanceState == null) {
changeFragment(new MainFragment(), false);
}
}