Невозможно изменить цвет фона кнопки - PullRequest
0 голосов
/ 11 июня 2019

Я не могу изменить цвет фона любого Button в моем приложении для Android, с помощью backgroundTint я могу, но тогда на кнопке не появляется значок, если сделать прозрачный оттенок.Вот код:

build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.abc"
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 121
        versionName "2.2.95"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-debug.pro'
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'com.google.code.gson:gson:2.7'
    implementation 'com.google.guava:guava:16.0.1'
    implementation 'androidx.annotation:annotation:1.0.0-beta01'
    implementation 'org.apache.commons:commons-io:1.3.2'

}
repositories {
    mavenCentral()
}

Activity.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_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context="com.abc.act">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="60dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:gravity="center"
            android:text="@string/app_name"
            android:textStyle="bold"
            android:textColor="#a8c476"
            android:textSize="32sp" />

    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_gravity="center">

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="35dip"
                android:layout_marginLeft="5dp"
                android:layout_weight="1.2"
                android:background="@drawable/custom_spinner">

                <Spinner
                    android:id="@+id/abc4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:drawSelectorOnTop="true"
                    android:entries="@array/protocolItems"
                    android:prompt="@string/protocol_default" />
            </FrameLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/abc1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="8.8"
                android:background="@drawable/custom_spinner"
                android:hint="@string/hint_text"
                android:inputType="text"
                android:maxLines="1"
                android:padding="5dp"
                android:textSize="18sp" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1.2"
                android:backgroundTint="@android:color/white"
                android:padding="5dp"

                app:icon="@android:drawable/ic_dialog_info" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_gravity="center">

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="35dip"
                android:layout_marginLeft="5dp"
                android:layout_weight="1.2"
                android:background="@drawable/custom_spinner">

                <Spinner
                    android:id="@+id/abc2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:drawSelectorOnTop="true"
                    android:entries="@array/spinnerItems"
                    android:prompt="@string/spinner_default" />
            </FrameLayout>
        </LinearLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            android:paddingBottom="3dp">

            <Button
                android:id="@+id/abc3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Enter"
                android:textColor="#ffffff"
                android:textSize="14sp" />
        </FrameLayout>

    </LinearLayout>
</LinearLayout>

Manifest.xml

<application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar">
</application>

У меня естьуже пробовал, меняя фон кнопки, меняя стиль темы.

Ответы [ 4 ]

1 голос
/ 11 июня 2019

Оттенок фона сделайте всю кнопку одинакового цвета.Вместо использования BackgroundTint вы должны использовать свойство Foreground android:foreground="@color/colorPrimary"

1 голос
/ 11 июня 2019

Для цвета фона используйте:

android:background="@android:color/white"

вместо:

android:backgroundTint="@android:color/white"

А для значка используйте:

android:drawableLeft="@drawable/ic_dialog_info"

вместо:

app:icon="@android:drawable/ic_dialog_info"
0 голосов
/ 11 июня 2019

Вы можете использовать это, чтобы добавить цвет и значок для вашей кнопки `` `

<Button
                    android:id="@+id/abc3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Enter"
                    android:textColor="#ffffff"
                    android:textSize="14sp"
                    android:background="@color/colorPrimary"
                    android:drawableLeft="@drawable/ic_launcher_foreground"
            />

Вы не можете использовать android: layout_weight = "1.2" внутри вашего FrameLayout, это неверно макет парам.

0 голосов
/ 11 июня 2019

вы можете использовать как следующий код:

<Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1.2"
            android:padding="5dp"
            android:drawableLeft="@android:drawable/ic_dialog_info"
            android:background="#000" />

использовать

android:drawableLeft=""

вместо

app:icon=""

и

background 

вместо

backgroundTint
...