首页 > 解决方案 > android布局单独的资源文件夹

问题描述

假设我在 res/layout 中有一个文件

<com.dog.cat.SomeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="Overdraw"
    android:id="@+id/SomeView"
    style="@style/SomeStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
/>

我想要一个类似 res/layout-sw600dp 的布局

<com.dog.cat.SomeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="Overdraw"
    android:id="@+id/SomeView"
    style="@style/SomeStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding_left="10dp"
/>

我只在第二个文件中添加了 android:padding_left="10dp" 。有没有办法将第一个文件添加到第二个文件而不像这样重新输入所有其他属性?

 <com.dog.cat.SomeLayout
    //include all attributes in one line
    android:padding_left="10dp"
/>

标签: android

解决方案


您可以创建另一种样式,例如:

<style name="SomeStyle.SixHundredDP">
    //Put other attributes here
    <item name="android:paddingLeft">10dp</item>
</style>

现在在 res/layout-sw600dp 中进行布局:

<com.dog.cat.SomeLayout
    //Other attributes for this view
    style="@style/SomeStyle.SixHundredDP"
/>

您也可以为不同的屏幕尺寸设置不同的值文件夹,就像布局一样。例如看到这个


推荐阅读