【发布时间】:2020-04-25 05:59:25
【问题描述】:
我想为按钮添加背景,我已经做了好几次了。我有一个具有适当背景的可绘制资源文件。但是,如果我链接它,它将不会被接受,而是只会使用一种颜色作为背景。 我怀疑这可能是我的 Styles.xml 中的一个错误,但在这些中没有任何东西被称为太彩色。 Android Studio 无法识别背景的原因是什么,或者我在哪里可以定义按钮外观的一般主题?我不记得曾经穿过那样的东西。我尝试了与 EditText 类似的东西,我在这里没有问题,所以我认为这是一个特定的按钮问题。 此外,如果我将背景颜色更改为纯白色,它不会改变任何东西,仍然显示为深蓝色。
更新:如果我将预览样式(预览的上部 middel)更改为 DialogStyle,按钮背景将起作用。如果 der 样式中没有定义任何内容,那怎么可能?
更新:这次我添加了一个新版本的按钮,以一种额外的样式初始化背景。结果是它仍然没有显示正确的颜色。我也试过黑色,但没有任何反应。
感谢您的帮助。
按钮
<Button
android:id="@+id/buttonRegistrieren"
android:layout_width="232dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="@drawable/btnbackground"
android:onClick="WriteData"
android:text="@string/Registrieren"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:textSize="16dp"></Button>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">@android:color/black</item>
</style>
<style
name="SplashTheme"
parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="ButtonStyle" parent="android:style/Widget.Button">
<item name="android:textSize">19sp</item>
<item name="android:textColor">@color/colorWhite</item>
<item name="android:background">@color/colorBlack</item>
</style>
<style
name="SecondTheme"
parent="AppTheme">
<item name="android:windowFullscreen">false</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
</resources>
btnbackground.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#FF5256AC"
android:endColor="#FF662D91"
android:startColor="#FF29ABE2"
android:type="linear" />
<corners android:radius="150dp">
</corners>
</shape>
带主题的按钮
<Button
android:id="@+id/buttonLogin"
android:layout_width="160dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Anmelden"
android:textAllCaps="true"
android:textColor="@color/colorWhite"
android:textSize="16sp"
android:theme="@style/ButtonStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="426dp"
/>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="edmt.dev.androideatitv2client">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:theme">
<activity android:name=".PasswordActivity"></activity>
<activity android:name=".LoginActivity" />
<activity android:name=".RegistrationActivity" />
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UpdatePasswordActivity" />
<activity android:name=".UpdateProfileActivity" />
<activity android:name=".ProfileActivity" />
<activity
android:name=".HomeActivity"
android:label="@string/title_activity_home"
android:theme="@style/AppTheme.NoActionBar" />
<service android:name=".services.MyFCMServices">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
【问题讨论】:
-
如果你发布你的布局会更好,因为这会产生效果,否则我测试了你的按钮和背景并且它们工作。样式与它无关,因为你没有在你的按钮..您是否尝试过双击预览选项卡或左上角同步?。
-
感谢您的回答!是的,我尝试过同步。我在一分钟前所做的是我将预览模式更改为 DialogStyle(看看我的样式),这使它工作。如果他们没有在styles.xml中声明,那怎么可能?
-
在常规的线性布局中尝试一下,我发誓它有效:)。
-
我自己尝试了这一切,它对我来说效果很好......我认为样式与它没有任何关系,因为您没有使用按钮引用样式。但是,您绝对可以尝试在样式中引用背景,然后为按钮提供该样式并查看是否发生任何不同
-
你能告诉我们你的后端吗?
标签: android xml android-studio android-layout button