android-styles 相关问题

样式是一组属性,用于指定视图或窗口的外观和格式

如何制作自定义输入pin码Android?

我在 Kotlin Android 上制作项目。我怎样才能使相同的字段和相同的输入?在我看来,创建一些编辑文本并不是一个好的决定 在此输入图像描述 输入图像描述她...

回答 1 投票 0


如何设置android操作栏选项卡的高度

基本上,我想更改操作栏中选项卡的高度。这个问题在 stackoverflow 上已经被问过好几次了,例如: ActionBar 选项卡高度 我已经尝试了大部分解决方案...

回答 2 投票 0

如何使样式从带有子项的自定义视图类中的布局“继承”?

我有一个名为card.xml 的布局,它是名为Card.kt 的自定义类的“基本”布局。我有一个样式(@style/homepage_block),它适用于基本情况。 卡.xml 我有一个名为 card.xml 的布局,它是名为 Card.kt 的自定义类的“基本”布局。我有一个适合它的风格(@style/homepage_block),它适用于基本情况。 card.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:divider="@drawable/separator_horizontal_empty_10" android:showDividers="middle" style="@style/homepage_block"> <!-- i set orientation and divider --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/block_title" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/homepage_block_title"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="TITLE" style="@style/text.Header" /> </androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout> 卡.kt package com.tempapplication.views import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import com.tempapplication.R import com.tempapplication.databinding.CardBinding class Card @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 ) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) { private val binding: CardBinding init { LayoutInflater.from(context).inflate(R.layout.card, this, true) binding = CardBinding.bind(this) initAttributes(attrs, defStyleAttr, defStyleRes) } // i have a custom attribute called title which i use to set the title text private fun initAttributes(attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) { if (attrs == null) { return } val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Card, defStyleAttr, defStyleRes) binding.title.text = typedArray.getString(R.styleable.Card_title) typedArray.recycle() } override fun addView(child: View?, params: ViewGroup.LayoutParams?) { super.addView(child, params) } } attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Card"> <attr name="title" format="string" /> </declare-styleable> </resources> 所以进一步的问题可以分成两部分。 第一个问题 我可以以某种方式将属性从card.xml“继承”到我使用此布局的自定义视图吗?例如,在 card.xml 中,我将线性布局方向设置为 vertical 并且还有一个自定义分隔线。但是当我在某些活动中包含自定义视图时,它似乎根本不知道它(是它应该是什么样子,以及它实际上是什么样子),所以我必须手动设置分隔符和包含此自定义视图时的方向。 activity.xml <com.tempapplication.views.Card android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:divider="@drawable/separator_horizontal_empty_10" android:showDividers="middle" app:title="PAGE OF WEEK" > <!-- i set orientation and divider even when they are set in card.xml --> <include layout="@layout/content_week" bind:data="@{data.week}"/> </com.tempapplication.views.Card> 也许我需要将分隔线和方向放入style资源中?还是有更优雅的方法来做到这一点? 第二个问题 我有子视图(如上面的代码中包含布局content_week)。如何使根线性布局 (card.xml) 的初始 @style/homepage_block 样式影响我包含在卡片视图中的子项?例如,我希望 Card 的所有子项都有红色背景,因为 card.xml 中使用了样式资源。有可能吗? content_week.xml <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="data" type="com.tempapplication.modules.HomepageBlockWeekData"/> </data> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:divider="@drawable/separator_horizontal_empty_10" android:showDividers="middle" style="@style/homepage_block_content"> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center_vertical" android:text="@{data.title}" style="@style/text.Header" /> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/_1pxh" android:backgroundTint="#FFCAC4D0" android:background="@drawable/separator" /> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@{data.description}" style="@style/text" /> </LinearLayout> </layout> 您的问题围绕在 Android 中创建自定义视图以及从包含的布局继承样式和属性进行。我将分别解决每个问题: 第一个问题:继承属性 在自定义 Card 视图中,您将根据 card.xml 中定义的自定义属性来扩充 attrs.xml 布局并设置属性。但是,直接在 XML 中为 android:orientation 视图的父布局设置的 android:divider、android:showDividers 和 Card 等属性不会自动应用于自定义视图。 如果您想从父 XML 继承这些属性,您可以通过修改您的 Card 类来实现: class Card @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 ) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) { // ... (other code remains the same) init { LayoutInflater.from(context).inflate(R.layout.card, this, true) binding = CardBinding.bind(this) // Apply attributes from XML to the custom view val attributes = context.obtainStyledAttributes(attrs, R.styleable.Card, defStyleAttr, defStyleRes) applyAttributes(attributes) attributes.recycle() initAttributes(attrs, defStyleAttr, defStyleRes) } private fun applyAttributes(attrs: TypedArray) { val orientation = attrs.getInt(R.styleable.Card_android_orientation, VERTICAL) val divider = attrs.getDrawable(R.styleable.Card_android_divider) val showDividers = attrs.getInt(R.styleable.Card_android_showDividers, SHOW_DIVIDER_NONE) orientation = VERTICAL // Force vertical orientation for the LinearLayout divider?.let { setDividerDrawable(divider) } setShowDividers(showDividers) } // ... (other code remains the same) } 在此代码中,我添加了一个 applyAttributes 函数,该函数从自定义属性获取的 android:orientation 中读取 android:divider、android:showDividers 和 TypedArray 等属性的值。然后,该函数将这些属性应用到自定义视图,确保继承父 XML 布局中设置的属性。 第二个问题:将样式应用于子视图 如果您希望 Card 自定义视图中的子视图继承 card.xml 布局中定义的样式,则需要为子视图设置适当的样式。在 content_week.xml 中,您可以通过在 style 属性中引用样式来应用样式: <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center_vertical" android:text="@{data.title}" style="@style/text.Header" /> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/_1pxh" android:backgroundTint="#FFCAC4D0" android:background="@drawable/separator" style="@style/homepage_block_content_separator" /> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@{data.description}" style="@style/text" /> 在这里,我向子视图添加了 style 属性以应用相关样式。确保在样式资源中定义这些样式(@style/text.Header、@style/homepage_block_content_separator等),并且它们应该应用于子视图。 请记住,设置样式时,您需要确保样式在样式资源中正确定义 (styles.xml),并且它们与您要应用它们的特定视图类型的属性和特性兼容。

回答 1 投票 0

是否可以将多种风格合而为一?

我有两种风格,称之为A和B。是否可以: 将它们应用到同一个 UI 元素,或者 将它们组合成一个单一的风格(基本上只是为了让我 完成...

回答 2 投票 0

在 Material3 中更改 ActionMode 背景颜色

从 Material3 开始,我无法更改动作模式背景颜色。 在 Material3 之前,我设法使用 actionModeBackground 属性更改背景颜色: 从 Material3 开始,我无法更改动作模式背景颜色。 在Material3之前,我设法用actionModeBackground属性改变了背景颜色: <item name="actionModeBackground">@color/some_color</item> 这是我的主题: <style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar"> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionModeOverlay">false</item> <item name="actionModeBackground">@color/action_mode_background</item> <item name="android:actionModeCloseDrawable">@drawable/ic_baseline_done_tick_24</item> </style> 我也检查了this线程但似乎不适用于material3. 如何在 Material3 上更改它? 谢谢!

回答 0 投票 0

如何为 React Native 应用程序创建响应式 UI 设计

如何考虑设备尺寸、屏幕方向和像素密度等因素,为 React Native 应用程序创建响应式 UI 设计?

回答 0 投票 0

android 中的全局样式规则未应用

我有一个普通的 Android 应用程序,无法理解为什么我的全局样式定义没有应用于视图元素。 这是我的文件 res/values/styles.xml: <question vote="0"> <p>我有一个普通的 Android 应用程序,无法理解,为什么我的全局样式定义没有应用于视图元素。</p> <p>这是我的文件<pre><code>res/values/styles.xml</code></pre>:</p> <pre><code>&lt;resources&gt; &lt;style name=&#34;Theme.XXXXX&#34; parent=&#34;Theme.MaterialComponents&#34;&gt; &lt;item name=&#34;android:textAppearance&#34;&gt;@style/Theme.XXXXX.TextAppearance&lt;/item&gt; &lt;/style&gt; &lt;style name=&#34;Theme.XXXXX.TextAppearance&#34; parent=&#34;TextAppearance.MaterialComponents.Body1&#34;&gt; &lt;item name=&#34;android:textSize&#34;&gt;24sp&lt;/item&gt; &lt;/style&gt; &lt;/resources&gt; </code></pre> <p>这是我在清单中全局设置样式的地方:</p> <pre><code>&lt;application android:dataExtractionRules=&#34;@xml/data_extraction_rules&#34; android:fullBackupContent=&#34;@xml/backup_rules&#34; android:icon=&#34;@drawable/ic_launcher&#34; android:label=&#34;@string/app_name&#34; android:supportsRtl=&#34;true&#34; android:theme=&#34;@style/Theme.XXXXX&#34;&gt; </code></pre> <p>这是一个琐碎的活动布局:</p> <p></p> <pre><code>&lt;LinearLayout android:layout_width=&#34;match_parent&#34; android:layout_height=&#34;match_parent&#34; android:orientation=&#34;vertical&#34; android:padding=&#34;@dimen/dialog_padding&#34;&gt; &lt;TextView android:id=&#34;@+id/introduction_overview&#34; android:layout_width=&#34;match_parent&#34; android:layout_height=&#34;wrap_content&#34; android:text=&#34;@string/introduction_overview&#34; /&gt; &lt;LinearLayout android:id=&#34;@+id/introduction_usage&#34; android:layout_width=&#34;match_parent&#34; android:layout_height=&#34;wrap_content&#34; android:orientation=&#34;vertical&#34;&gt; </code></pre> <p>我的期望是TextView“@+id/introduction_overview”继承样式定义中定义的字体大小。然而,情况并非如此。为什么不呢?<em> </em>在我测试时,我将显式样式</p><code>android:textAppearance=&#34;@style/Theme.XXXXX.TextAppearance&#34;</code><p>添加到<pre><code>TextView</code></pre>。行得通,字体大小确实得到了应用。但我不想为我所有的视图元素设置单独的样式!这就是全局样式定义的目的!<pre> </pre>我看不出在哪里可以覆盖我自己的样式定义。或其他样式定义的任何显式应用。这是一个简单的应用程序。</p> <p>有谁知道我可能会检查什么,我可能需要更改什么?</p> <p> </p></question>

回答 0 投票 0

.Net Maui 应用程序在 Play Store 时如何解决不同的颜色?

我有 .Net 7 Maui 应用程序。 当应用程序从 Visual Studio 部署到物理设备时,顶部栏的颜色设置正确。 然而,当完全相同的项目应用程序发布到 Play 商店时,...

回答 0 投票 0

Android TextInputLayout 和 TextInputEditText 光标颜色

我想更改光标(或插入符号)颜色,就像在这个堆栈溢出问题中一样,但是在材料 3 中。 我想更改光标(或插入符号)颜色,就像在这个堆栈溢出问题中一样,但是在 Material 3 中。 <com.google.android.material.textfield.TextInputLayout android:id="@+id/brands_input_layout" style="@style/Widget.Material3.TextInputLayout.FilledBox.ExposedDropdownMenu.Honk" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/size_medium" android:paddingEnd="44dp" android:theme="@style/Widget.Material3.TextInputLayout.FilledBox.ExposedDropdownMenu.Honk" app:layout_constraintEnd_toStartOf="@id/hide_show_brands_icon" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:ignore="RtlSymmetry"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/description_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:imeOptions="actionDone" android:inputType="textCapSentences" /> </com.google.android.material.textfield.TextInputLayout> 这是我的主题文件: <style name="Widget.Material3.TextInputLayout.FilledBox.ExposedDropdownMenu.Honk"> <item name="boxStrokeColor">@color/textDropdownBoxStrokeColor</item> <item name="hintTextColor">@color/textDropdownBoxStrokeColor</item> <item name="colorControlActivated">@color/textDropdownBoxStrokeColor</item> <item name="endIconTint">@color/dropdown_menu_arrow</item> </style> 我已经试过了: <style name="ThemeOverlay.SmartHabit.TextInputEditText.FilledBox.Dense" parent="ThemeOverlay.Material3.TextInputEditText.FilledBox.Dense"> <item name="colorControlActivated">@color/colorSecondary</item> </style> <com.google.android.material.textfield.TextInputEditText android:id="@+id/description_input" style="@style/ThemeOverlay.SmartHabit.TextInputEditText.FilledBox.Dense" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:imeOptions="actionDone" android:inputType="textCapSentences" /> 我也已经试过了: <style name="ThemeOverlay.SmartHabit.TextInputEditText.FilledBox.Dense" parent="ThemeOverlay.Material3.TextInputEditText.FilledBox.Dense"> <item name="colorPrimary">@color/colorSecondary</item> </style> <com.google.android.material.textfield.TextInputEditText android:id="@+id/description_input" style="@style/ThemeOverlay.SmartHabit.TextInputEditText.FilledBox.Dense" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:imeOptions="actionDone" android:inputType="textCapSentences" /> 编辑: 根据Material 3 Documentation,我已经尝试这样做了,但是没有用: <com.google.android.material.textfield.TextInputEditText android:id="@+id/description_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:imeOptions="actionDone" android:inputType="textCapSentences" app:cursorColor="@color/myColor" />

回答 0 投票 0

不能在 android 的样式标签中使用背景元素

我正在尝试使用样式将自定义样式应用于android中的textview。但不幸的是我在运行时遇到了这个异常。 android.view.InflateException:com.m 中的二进制 XML 文件行 #59 ...

回答 0 投票 0

如何通过Java/Kotlin代码改变colorPrimary值?

我想在单击某个按钮时更改 colorPrimary 值。但我不知道如何实现它。 <item name=&</desc> <question vote="0"> <p>我想在单击某个按钮时更改 colorPrimary 值。但我不知道如何实现它。</p> <pre><code> &lt;style name=&#34;Theme.Life.Light&#34; parent=&#34;***&#34; &gt; &lt;item name=&#34;colorPrimary&#34;&gt;@color/colorPrimary&lt;/item&gt; &lt;item name=&#34;colorSecondary&#34;&gt;@color/colorSecondary&lt;/item&gt; &lt;item name=&#34;colorOnPrimary&#34;&gt;@color/colorOnPrimary&lt;/item&gt; &lt;item name=&#34;colorOnSecondary&#34;&gt;@color/colorOnSecondary&lt;/item&gt; &lt;/style&gt; </code></pre> <p>就像coede show一样,我想通过Java/Kotlin代码改变<strong>colorPrimary</strong></p> <p>我该怎么办?</p> </question> <answer tick="true" vote="0"> <p>这些颜色在主题中定义,不能以编程方式更改。相反,您可以手动更改颜色或单独更改特定视图的颜色。</p> </answer> </body></html>

回答 0 投票 0

无法设置状态变化按钮的背景颜色_启用。

我有一个输入电话号码的字段,并为其设置了掩码。我希望按钮的颜色是,例如,当掩码还没有被完全填满时,按钮的颜色是黑色,而当它被填满时,按钮的颜色是黄色。我的按钮

回答 1 投票 0

当应用带有数据绑定的主题时,编译器错误。

我的代码为ColorBox对象的列表无限期地洗牌颜色和索引。这是我的观点。

回答 1 投票 1

不能使用主题改变按钮背景

我想在我的应用程序中为一个Button设置自定义样式,但我尝试设置一个自定义主题,但没有成功。例如,这并没有改变背景

回答 1 投票 0

如何找到EditText的默认样式?

我想看看EditText的默认样式到底是怎么定义的。所以对我来说,第一步就是要弄清楚,到底哪个样式是默认的。检查EditText源代码,我发现:......

回答 1 投票 0

带有MaterialShapeDrawable的MaterialCardView。

我想实现这个目标。所以我想我可以用MaterialShapeDrawable来应用这个转换: binding.card.background = MaterialShapeDrawable( ShapeAppearanceModel.builder() ......

回答 1 投票 0

如何在Android中使用Material Design在EditText和TextView中应用shapeAppreanace。

我想设计一个带圆角的TextView和EditTextView。有一个直接的解决方案。使用自定义形状背景。但是由于material design 1.1.0引入了......。

回答 1 投票 1

如何在Android中使用Material Design在EditText和TextView中应用shapeAppreanace。

我想设计一个带圆角的TextView和EditText。有一个直接的解决方案。使用自定义形状背景。但由于material design 1.1.0引入了shapeAppearance......。

回答 2 投票 2

如何对Android MaterialCalendars各个组件进行风格化处理?

我正在尝试对MaterialCalendar进行样式设计,但我没有任何进展,希望得到任何帮助。我的style.xml看起来类似于下面。

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.