消除 xml 中的属性供过于求

Get rid of attributes oversupply in xml

如何在 xml 布局中最小化属性名称的数量?

例如我有这个,里面装满了必要的东西:

<ScrollView
android:id="@+id/scroll"
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:scrollbars="none"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

谢谢!

使用 style.

A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.

Styles in Android share a similar philosophy to cascading stylesheets in web design—they allow you to separate the design from the content.

在您的 styles.xml 值文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="YourScrollView" >
        <item name="android:layout_width">match_parent</item>
        <item name="android:scrollbars">none</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:paddingBottom">@dimen/activity_vertical_margin</item>
        <item name="android:paddingLeft">@dimen/activity_horizontal_margin</item>
        <item name="android:paddingRight">@dimen/activity_horizontal_margin</item>
        <item name="android:paddingTop">@dimen/activity_vertical_margin</item>
    </style>
</resources>

然后你的布局变成:

<ScrollView
    android:id="@+id/scroll"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main"
    style="@style/YourScollView"
    >