【问题标题】:Setting the background using styles使用样式设置背景
【发布时间】:2019-04-07 14:28:03
【问题描述】:

如何使用styles.xml 设置布局可绘制背景?在布局中,我使用了 2 个可绘制按钮。

当我在 styles.xml 中使用它时,所有按钮都会将背景更改为样式中的内容,但我只想更改布局背景。

<style name="menutheme">
    <item name="android:background">@drawable/menu</item>
</style>

我的布局:

<android.support.constraint.ConstraintLayout 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_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/menutheme"
android:contentDescription="@null"
tools:context="com.example.user.app.Activity2">


<RelativeLayout
    android:layout_width="312dp"
    android:layout_height="371dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.955">

    <Button
        android:id="@+id/entrycity"
        android:layout_width="243dp"
        android:layout_height="65dp"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:background="@drawable/oval3"
        android:fontFamily="@font/ultra"
        android:text="@string/wybor2"
        android:textColor="@android:color/background_light"
        android:textSize="30sp"
        tools:layout_editor_absoluteX="59dp"
        tools:layout_editor_absoluteY="334dp" />

    <Button
        android:id="@+id/button3"
        android:layout_width="243dp"
        android:layout_height="65dp"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/entrycity"
        android:background="@drawable/oval"
        android:fontFamily="@font/ultra"
        android:text="@string/wybor"
        android:textColor="@android:color/background_light"
        android:textSize="30sp"
        tools:layout_editor_absoluteX="71dp"
        tools:layout_editor_absoluteY="227dp" />

        </RelativeLayout
        </android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: java android background styles


    【解决方案1】:

    尝试WindowBackground 而不是背景。喜欢,

    <style name="menutheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/menu</item>
    </style>
    

    并且您需要将此主题应用于清单中的活动。而不是布局中。

    【讨论】:

    • 我想为每个布局设置不同的背景
    • 那你为什么用主题来提供背景?直接将背景应用于布局。
    猜你喜欢
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2023-03-29
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多