在Android Studio项目中没有dimens.xml文件。

18

我已在我的笔记本电脑上安装了Android Studio 2.2.2。然后最近更新了它。但是当我创建一个空项目时,项目中没有dimens.xml文件。而当我使用Android Studio 2.2.2时,有一个dimens目录(带有2个dimens.xml文件)。我的Android Studio出了什么问题?


2
你可以在values文件夹中自己创建它。 - miljon
那么,我的Android Studio出了问题吗? - dev-x
很难说,但尝试将其更新到最新版本并重新启动Android Studio,并创建新项目。但是,如果没有(dimen文件),您可以只需创建它并开始工作 ;) - miljon
如果您需要一个dimens.xml文件,只需在res/values文件夹下创建一个即可。 - Gabriele Mariotti
1个回答

24

你的Android Studio没问题。从2.3版本开始,默认的Activity布局模板使用ConstraintLayout作为根元素,没有应用任何边距。在旧版模板中,这通常是一个RelativeLayout,其边距是在dimens.xml中设置的资源值。由于这些值不再包含在默认的布局文件中,因此默认情况下项目中不会创建一个空的dimens.xml文件。

如果您需要一个dimens.xml文件,您只需在res/values文件夹下创建一个带有New -> Values resource file的文件即可。


供参考的旧默认布局,使用了dimens资源:

<?xml version="1.0" encoding="utf-8"?>
<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.package.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</LinearLayout>

而新的默认设置不会:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.package.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

返回翻译文本: true但是其中的原因是什么,以及如何在创建项目时自动创建它。 - Rakesh Yadav
这就是新模板的工作方式。从现在开始,您必须自己创建dimens.xml - zsmb13
2
这方面有官方文档吗? - Rakesh Yadav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接