从 XML 读取维度



我已经阅读并尝试了一些我在stackoverflow中找到的答案,但我找到的答案不起作用。

我有以下尺寸(纵向按钮高度\宽度为 1+2,横向为 3+4(。

我只希望按钮大小在横向上比纵向小

<dimen name="button_height_portrait">180dp</dimen>
<dimen name="button_width_portrait">180dp</dimen>
<dimen name="button_height_landscape">60dp</dimen>
<dimen name="button_width_landscape">60dp</dimen>

在我的代码中,我覆盖了配置更改:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mScreenOrientation == Configuration.ORIENTATION_PORTRAIT) {
           if (mButton != null) {
               Integer ButtonWidthPortrait = getResources().getDimensionPixelSize(R.dimen.button_width_portrait);
               Integer ButtonHeightPortrait = getResources().getDimensionPixelSize(R.dimen.button_height_portrait);
               mButton.setWidth(ButtonWidthPortrait);
               mButton.setHeight(ButtonHeightPortrait);
           }
        }
        else if (mScreenOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            if (mButton!= null) {
                Integer ButtonWidthLandscape = getResources().getDimensionPixelSize(R.dimen.button_width_landscape);
                Integer ButtonHeightLandscape = getResources().getDimensionPixelSize(R.dimen.button_height_landscape);
                mButton.setWidth(ButtonWidthLandscape);
                mButton.setHeight(ButtonHeightLandscape);
            }
        }
    }

结果是按钮看起来大小完全相同。我还尝试获取维度:

int dimenHeightPortrait = (int) (getResources().getDimension(R.dimen.start_button_height_portrait) / getResources().getDisplayMetrics().density)

等等......但结果是一样的。

我在配置更改的基方法中更新了 mScreenOrientation:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    mScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
}
else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    mScreenOrientation = Configuration.ORIENTATION_LANDSCAPE;
}

这是我的完整文件:

<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="nav_header_vertical_spacing">16dp</dimen>
    <dimen name="nav_header_height">160dp</dimen>
    <dimen name="fab_margin">16dp</dimen>
    <dimen name="star_text_size">26sp</dimen>
    <dimen name="toast_save_text_size">34sp</dimen>
    <dimen name="toast_image_height">50dp</dimen>
    <dimen name="toast_image_width">50dp</dimen>
    <dimen name="space_between_touch_views">10dp</dimen>
    <dimen name="button_moretext_size">20sp</dimen>
    <dimen name="button_get_text_size">20sp</dimen>
    <dimen name="key_corner_radius">3dp</dimen>
    <dimen name="key_top_inset">3dp</dimen>
    <dimen name="key_bottom_inset">3dp</dimen>
    <dimen name="key_right_inset">2.5dp</dimen>
    <dimen name="key_left_inset">2.5dp</dimen>
    <dimen name="title_text_size">11sp</dimen>

    <dimen name="button_height_portrait">180dp</dimen>
    <dimen name="button_width_portrait">180dp</dimen>
    <dimen name="button_height_landscape">60dp</dimen>
    <dimen name="button_width_landscape">60dp</dimen>
</resources>

成功了!

我不知道为什么 setWidth 和 setHeight 不起作用,但这有效(所以如果有人也遇到这个问题,那么只需将 setWidth 和 setHeight 的两行替换为这些下一行,并对肖像执行相同的操作(:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.width = ButtonWidthLandscape;
params.height = ButtonHeightLandscape;
mButton.setLayoutParams(params);

相关内容

  • 没有找到相关文章

最新更新