如何以编程方式改变一个应用程序的大小,以DIP,而不是PX [英] How to programmatically alter an apps size, with DIP instead of PX

查看:135
本文介绍了如何以编程方式改变一个应用程序的大小,以DIP,而不是PX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜查,发现答案,这样的一个特殊的量,但没有将展示他们是如何插入的活动。

I've searched and found an exceptional amount of answers to this, but none that will show how they are inserted into the activity.

我的活动都有一个tabsadapter和viewpager(fragmentpageradapter)。而已。而且我已经将它设置为被称呼为一个对话框,像这样

My activity has a tabsadapter and viewpager (fragmentpageradapter). That's it. And I've set it to be styled as a dialog, like this

    <style name="Theme.PopupTheme" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowActionBar">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:padding">2sp</item>
    <item name="android:divider">@drawable/transparent</item>

现在,这只不过它采用了显示器的全尺寸勉强两侧和底部的任何透明度工作得很好。没有布置这项活动,只是一个viewpager XML

Now, this works just fine except that it takes up the full size of the display with barely any transparency on the sides and bottom. There is no layout for this activity, just a viewpager xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

由于这个原因,我绑编程上浆应用。我发现我只能管理本(与我的技术水平)与像素设置它。这可好看了,如果它是为那些有一定的设备,但比起我的S3应用程序看起来我的Nexus 10残暴根据我设置的像素数量时,反之亦然。

Because of this I'm tied to sizing the app programmatically. I found that I can only manage this (with my level of skill) by setting it with pixels. Which can look good if it was meant for a certain device, but the app looks atrocious on my Nexus 10 compared to my S3 when and vice-versa depending on the amount of pixels I set.

我怎样才能做到这一点编程方式与浸?

How can I do this programmatically with dip?

推荐答案

我不认为有一种方法可以直接在浸设置。

I don't think there's a way to set it directly in dip.

您可以使用此方法从DIP转换为像素,并使用结果大小的看法。

You can use this method to convert from dip to px and use the result to size the view.

private static float scale = 0;
...
public static int dps2pixels(int dps, Context context) {
    if (0 == scale) {
        scale = context.getResources().getDisplayMetrics().density;
    }
    return (int) (dps * scale + 0.5f);
}

这篇关于如何以编程方式改变一个应用程序的大小,以DIP,而不是PX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆