【问题标题】:Errors on @dimen@dimen 上的错误
【发布时间】:2017-08-30 17:16:41
【问题描述】:

我是 Android Studio 新手。我在@dimen 中有错误,我不知道如何解决。

代码如下:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.videotest.MainActivity">

【问题讨论】:

  • 可能不存在?添加堆栈跟踪
  • 您没有显示代码,也没有显示错误 - 如果没有适当的上下文,我们应该如何帮助您?包括所有相关详细信息,以获得更快和/或更高质量的帮助。

标签: java android


【解决方案1】:

您必须在dimens.xml 中通过@dimen 定义您设置的尺寸。

根据 Android Docs,“维度是使用 name 属性中提供的值(不是 XML 文件的名称)引用的简单资源。因此,您可以将维度资源与其他简单资源一个 XML 文件,在一个元素下。”

来自 Android 文档,dimens.xml 的示例:

<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2013 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<resources>
    <dimen name="action_button_min_width">56dp</dimen>
    <dimen name="indeterminate_progress_size">32dp</dimen>
</resources>

在此处定义您的其他维度并将此 XML 保存在 values 目录下。也看看这个解释when to use the dimens.xml file in Android?

的答案

【讨论】:

    【解决方案2】:

    首先@dimen 指的是尺寸,它是一个文件,您可以在其中定义尺寸,以便以后在任何布局文件中使用它们。

    在您的项目结构中,转到目录 app/src/main/res/values/ 现在在这里检查是否存在dimens.xml 文件。

    如果没有,则创建一个名为 dimens.xml 的新资源文件

    因此,根据您想要的填充来定义这样的尺寸:

    <resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    </resources>
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 1970-01-01
      • 2019-07-29
      • 2017-04-22
      • 2016-09-05
      • 2020-06-15
      • 1970-01-01
      • 2023-01-20
      • 2021-01-04
      相关资源
      最近更新 更多