无法更改按钮的背景色

时间:2019-06-11 10:54:03

标签: android android-button androidx

我无法通过使用private OpenIdConnectAuthenticationOptions CreateOptionsFromPolicy(string policy) { return new OpenIdConnectAuthenticationOptions { // set the authentication type to the id of the policy MetadataAddress = metaDataAddress, AuthenticationType = policy, // These are standard OpenID Connect parameters, with values pulled from web.config ClientId = clientId, //ClientSecret = clientSecret, RedirectUri = redirectUri, PostLogoutRedirectUri = redirectUri, Notifications = new OpenIdConnectAuthenticationNotifications { AuthenticationFailed = AuthenticationFailed, }, Scope = "openid", ResponseType = "id_token", // used for displaying the user's name in the navigation bar. TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name", RoleClaimType = "role", SaveSigninToken = true } }; } 来更改我的Android应用程序中任何Button的背景颜色,但是如果使颜色透明,则按钮上将不会显示任何图标。这是代码:

build.gradle:

backgroundTint

Activity.xml

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()
}

Manifest.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>

我已经尝试过更改按钮的背景,更改主题的样式。

4 个答案:

答案 0 :(得分:1)

要使用背景色,请使用:

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

而不是:

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

对于图标,请使用:

android:drawableLeft="@drawable/ic_dialog_info"

代替:

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

答案 1 :(得分:1)

  

背景色使整个按钮具有相同的颜色。而不是使用BackgroundTint,您应该使用Foreground属性android:foreground="@color/colorPrimary"

答案 2 :(得分:0)

您可以使用以下代码:

<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

答案 3 :(得分:0)

您可以使用它为按钮添加颜色和图标     ```

<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"
            />

您不能在FrameLayout中使用android:layout_weight =“ 1.2”,这是无效的 布局参数。