gpt4 book ai didi

android - 摆脱 xml 中过多的属性

转载 作者:行者123 更新时间:2023-11-30 01:46:31 25 4
gpt4 key购买 nike

如何减少 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.

在 values 文件夹中的 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"
>

关于android - 摆脱 xml 中过多的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33639749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com