Android 用户界面规模

标签 android android-layout android-design-library androiddesignsupport

我知道 Android 系统提供了一种在整个系统上扩展 UI 的方法,但我想要类似的东西,但只适用于我的应用程序。假设在“设置”中我有一个缩放 UI 的选项,并且使用 slider 我的应用程序的 UI 只能在我的应用程序上放大或缩小。有什么想法可以轻松做到这一点吗?

更新:

我已经在 onCreate 中试过了:

private void setScale(float screenRatio) {
    Resources resources = getResources();
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    Configuration configuration = resources.getConfiguration();
    configuration.densityDpi = (int) (metrics.densityDpi * screenRatio);
    metrics.scaledDensity = metrics.scaledDensity * screenRatio;
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}

但存在三个缺点:

  1. 我需要重新启动应用程序才能使更改生效
  2. 它只缩放 View 而不缩放字体大小
  3. 变化不是实时可见的

这是相同的 260dpi 屏幕,但具有缩放的 UI。

/image/wkzZb.png

/image/WF5rY.png

/image/GMYRA.png

/image/hTREN.png

最佳答案

这就是我找到的解决方案,这里是一个如何实现它的工作示例: https://github.com/thelong1EU/ScaleUI

<强>1。创建一个使用自定义配置对象的 ContextWraper 对象

正如我所猜测的那样,这一切都与上下文对象有关。第一步是创建一个包含所需修改的 ContextWraper 对象。您可以从此处更改语言环境和所有其他限定符或设备属性。请在此处查看完整列表: https://developer.android.com/reference/android/content/res/Configuration.html#lfields

public class ScaledContextWrapper extends ContextWrapper {

    public ScaledContextWrapper(Context base) {
        super(base);
    }

    @SuppressWarnings("deprecation")
    public static ScaledContextWrapper wrap(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        DisplayMetrics metrics = resources.getDisplayMetrics();

        configuration.densityDpi = (int) (metrics.densityDpi * sScaleRatio);
        configuration.fontScale = sScaleRatio;

        if (SDK_INT > 17) {
            context = context.createConfigurationContext(configuration);
        } else {
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }

        return new ScaledContextWrapper(context);
    }

}

<强>2。设置您希望进行修改的新上下文

如果你想改变整个 Activity 的比例,你需要覆盖 attachBaseContext() 并传递修改后的 Context 对象:

@Override
protected void attachBaseContext(Context newBase) {
    Context context = ScaledContextWrapper.wrap(newBase);
    super.attachBaseContext(context);
}

如果您只想更改一个 fragment 甚至一个 View 的比例,您需要使用使用修改后的 Context 构造的 LayoutInflater 对象。这是缩放 fragment 的示例:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    Context scaledContext = ScaledContextWrapper.wrap(getActivity());
    LayoutInflater scaledInflater = LayoutInflater.from(scaledContext);

    return scaledInflater.inflate(R.layout.fragment_layout, container, false);
}

关于Android 用户界面规模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42898179/

相关文章:

android - 将 drawableLeft 和 drawableRight 的高度约束为 TextView 的高度

android - android-support-v4 版本中的 fragment ID。 22

android - 如何捕捉按钮上的点击事件?

java - 如何制作带有角半径和起始颜色的android edittext样式?

Android TabLayout Android 设计

android - BottomNavigationView 与 RecyclerView 垂直滚动的垂直滚动 CoordinatorLayout 行为

java - 如何正确迭代 HashMap 元素的数组列表

xml - Android Studio 3.2.1 未正确缩进布局 XML

java - android.view.InflateException :Error Inflating Class

android - 底页横向问题