[英]styles.xml is not working as intended
Context: 语境:
I recently started trying to learn how to develop very simple Android apps, and have been following many different tutorials and resources available online. 我最近开始尝试学习如何开发非常简单的Android应用程序,并且一直在关注许多在线提供的不同教程和资源。 I was looking at an exercise that showed how you can use additional
xml
files, such as styles.xml
to apply a certain style to something without having to copy and paste the same code in the main.axml
multiple times. 我正在看一个练习,该练习显示了如何使用其他
xml
文件(例如styles.xml
将某种样式应用于某项内容,而不必多次将同一代码复制并粘贴到main.axml
。 So for example, if you have many Buttons, you can apply one general style to all those buttons. 因此,例如,如果您有许多按钮,则可以将一种常规样式应用于所有这些按钮。
Problem: I have created this app that does nothing, I just wanted to experiment with combing layouts and getting a feel for things. 问题:我创建了一个不执行任何操作的应用程序,我只是想尝试组合布局和感受事物。 It has 4 buttons (that don't click as yet).
它具有4个按钮(尚未单击)。 This is how it looks, followed by the
main.axml
code. 看起来就是这样,后面是
main.axml
代码。
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/calculator_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="50sp"
android:text="0" />
</HorizontalScrollView>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:columnCount="2"
android:rowCount="3">
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="#ff3f51b5"
android:textSize="25sp"
android:text="1st"
android:onClick="ButtonClick" />
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="#F44336"
android:textSize="25sp"
android:text="2nd"
android:onClick="ButtonClick" />
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="#ff9c27b0"
android:textSize="25sp"
android:text="3rd"
android:onClick="ButtonClick" />
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:background="#ff4caf50"
android:textSize="25sp"
android:text="4th"
android:onClick="ButtonClick" />
</GridLayout>
</LinearLayout>
Now I tried applying a 'style' to the buttons because they have a lot of the same properties, and it will make the code simpler and easier to read. 现在,我尝试对按钮应用“样式”,因为它们具有很多相同的属性,这将使代码更简单易读。 This is how I applied the style:
这就是我应用样式的方式:
<Button
style="@style/button_style"
android:background="#ff3f51b5"
android:text="1st"/>
<Button
style="@style/button_style"
android:background="#F44336"
android:text="2nd"/>
<Button
style="@style/button_style"
android:background="#ff9c27b0"
android:text="3rd"/>
<Button
style="@style/button_style"
android:background="#ff4caf50"
android:text="4th"/>
And this is what's in styles.xml
: 这就是
styles.xml
:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="button_style">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_rowWeight">1</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:textSize">25dp</item>
<item name="android:onClick">ButtonClick</item>
</style>
</resources>
And finally how the UI looks. 最后是用户界面的外观。 As evident, none of the button attributes from
styles
got carried over : 显而易见,
styles
中的按钮属性都没有被继承:
I can't seem to figure out what I'm doing wrong. 我似乎无法弄清楚我在做什么错。 I know I probably won't ever need to do this with buttons but I guess it could be helpful for something else later on.
我知道我可能永远都不需要使用按钮来执行此操作,但是我想这可能对以后的其他操作有所帮助。
Make your button_style extend as 使您的button_style扩展为
parent="Widget.AppCompat.Button"
full style would be 完整的风格将是
<style name="button_style" parent="Widget.AppCompat.Button">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_rowWeight">1</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:textSize">25dp</item>
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.