简体   繁体   English

如何实际重用UI布局元素?

[英]How to actually re-use UI layout elements?

To make sure every UI element of the same type looks the same, I am trying to find a way to re-use layout elements for proper way. 为了确保相同类型的每个UI元素看起来都相同,我试图找到一种以适当方式重用布局元素的方法。 For example, my primary CTA button always has a rounded background, font, text size, spacing, color, etc. 例如,我的主要CTA按钮始终具有圆形的背景,字体,文本大小,间距,颜色等。

I could use the <include/> tag like so: 我可以这样使用<include/>标签:

<include
    layout="@layout/include_primary_button"
    android:id="@+id/activity_home_cta" />

with my include_primary_button.xml being: 与我的include_primary_button.xml是:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <Button
        tools:ignore="MissingPrefix"
        fontPath="fonts/Roboto.ttf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button"
        android:text="STUB!"
        android:textAllCaps="true"
        android:textColor="@color/text"
        android:textSize="@dimen/text" />
</merge>

Using the <include/> tag limits only the layout properties to be overridden. 使用<include/>标记仅限制布局属性被覆盖。 So for example setting the text of every button used, I need to findViewById in the activity/fragment and set the text programmatically. 因此,例如设置每个使用的按钮的文本,我需要在活动/片段中找到findViewById并以编程方式设置文本。 This annoys me, but can be overcome. 这让我很烦,但是可以克服。 But then retrieving the button from the activity (eg. Button mButton = findViewById(R.id.activity_home_cta); ), the button is null. 但随后从活动中检索按钮(例如, Button mButton = findViewById(R.id.activity_home_cta); ),则该按钮为null。 What is missing here? 这里缺少什么?

And is there not an easier way to re-use elements? 还有没有更简单的方法来重用元素? This really feels like a hassle. 这真的感觉很麻烦。

You can make a style. 您可以制作样式。 For example 例如

<style name="CustomButton" parent="@style/Widget.AppCompat.Button">
        <item name="android:minHeight">40dp</item>
        <item name="android:textAppearance">@style/ButtonText</item>
        <item name="android:textColor">@color/btn_primary</item>
        <item name="android:background">@drawable/btn_primary</item>
        <item name="android:paddingEnd">@dimen/btn_padding</item>
        <item name="android:paddingStart">@dimen/btn_padding</item>
    </style>

 <style name="ButtonText">
        <item name="android:textAllCaps">true</item>
        <item name="android:fontFamily">@font/arial</item>
        <item name="android:textSize">@dimen/text_button</item>
    </style>

And then add it 然后添加

<Button
            style="@style/CustomButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn" />

If you want to have a custom view you can always create a new class that will extend from whatever you want (ex, Button, View) which will have its own xml and then you would add it like so. 如果您想拥有一个自定义视图,则始终可以创建一个新类,该类将从您想要的任何内容(例如,Button,View)扩展而来,并具有自己的xml,然后您可以像这样添加它。 (Here is a tutorial for that. There are many on the interwebz) (这是一个教程 。interwebz上有很多)

<CustomView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

Use styles. 使用样式。 For simple use cases like the one you say custom styles will help. 对于像这样的简单用例,您说自定义样式会有所帮助。

One thing you've got to know is the following: 您必须了解的一件事是:

The <include> tag is not a real view, so findByView will not find it. <include>标记不是真实视图,因此findByView将找不到它。 The @id attribute (and any other attributes you've set on the include tag) gets applied on the root tag of the included layout instead. @id属性(以及您在include标记上设置的任何其他属性)将改为应用于包含布局的根标记。 (source) (资源)

Meaning that your id attribute gets transferred to the <merge> tag, which isn't a real view, hence why it can't be found. 这意味着您的id属性将被转移到<merge>标记,这不是真实视图,因此为什么找不到它。 If you remove the <merge> tag, it should work. 如果删除<merge>标记,它将起作用。

Also, instead of creating that button in a layout file and including it, you could also create a custom View and then use that in your layout files. 另外,除了在布局文件中创建该按钮并将其包含之外,您还可以创建一个自定义View ,然后在布局文件中使用它。 You can read more about creating a View here . 您可以在此处阅读有关创建View更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM