簡體   English   中英

如何在android中創建自定義布局?

[英]How can I create custom layout in android?

我在不止視圖中重復了容器。
我怎樣才能一次創建這個容器並使用構建器設計模式實現它的類。

示例:我根據片段更改了 TextView 的顏色和文本。

嗨,您的 textView 的通用布局將其命名為 layout_text_view.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <variable
        name="dark"
        type="Boolean" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtTitle"
        android:text="Some Random Text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawablePadding="@dimen/padding_medium"
        android:textColor='@{dark?@color/white:@color/colorBlack}'
        android:textSize="@dimen/txt_xxxx_large"
        app:fontFamily="@font/sfprotext_bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

你可以在你的片段中使用這個布局,像這樣包含標簽

<include
        android:id="@+id/toolbar"
        layout="@layout/layout_toolbar"
        app:dark='@{true}'
        />

您可以在數據標記中使用許多值,這將更改您的常用ayout_text_view.xml 文件。

注意-我假設您可能在項目中使用數據綁定,如果沒有,則粘貼

dataBinding {
    enabled true
}

在您的 build.gradel(App level) 中。 在安卓標簽中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM