【问题标题】:Why my Button's background color changed to gray upon Running the app?为什么我的 Button 的背景色在运行应用程序时变为灰色?
【发布时间】:2020-11-23 18:42:36
【问题描述】:

我的代码一定有问题,将按钮背景颜色更改为带有黑色文本的灰色,并且在运行应用程序进行测试时也更改了它们的大小。我无法指出哪里出了问题。请帮忙。这可能是我的代码和 AdMob 之间的冲突,也可能是代码的结构。

package com.example.kakurosidekick;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

import java.util.Arrays;
public class MainActivity extends Activity implements View.OnClickListener {

ListView list;
    String[] kakNmbrs_25_5 = {"1+2+5+8+9", "1+2+6+7+9", "1+3+4+8+9", "1+3+5+7+9", "1+3+6+7+8", "1+4+5+6+9", "1+4+5+7+8",
            "2+3+4+7+9", "2+3+5+6+9", "2+3+5+7+8", "2+4+5+6+8", "3+4+5+6+7"};
    String mstrButtn = String.valueOf(0);

        private AdView mAdView;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn3 = (Button) findViewById(R.id.button3);
            Button btn4 = (Button) findViewById(R.id.button4);
           Button btn25 = (Button) findViewById(R.id.button25);
            Button dbtn2 = (Button) findViewById(R.id.DigiB2);
           btn3.setOnClickListener(this); // calling onClick() method
            btn4.setOnClickListener((View.OnClickListener) this);
            btn25.setOnClickListener(this);
            dbtn2.setOnClickListener((View.OnClickListener) this);
            list = findViewById(R.id.myList);
            Arrays.sort(kakNmbrs_25_5);
            
            MobileAds.initialize(this, new OnInitializationCompleteListener() {
                
                public void goToAnActivity(View view) {

                }
                public void onInitializationComplete(InitializationStatus initializationStatus) {
                }
            });

            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
        }

    @Override
    public void onClick(View v) {
        Button btn03 = (Button) findViewById(R.id.button3);
        Button btn04 = (Button) findViewById(R.id.button4);
        Button btn05 = (Button) findViewById(R.id.button5);
        Button btn06 = (Button) findViewById(R.id.button6);

        Button[] buttonArray = {btn05, btn04, btn03};

        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.mytextsize, android.R.id.text1, kakNmbrs_25_5);

        switch (v.getId()) {

            case R.id.button3:
                mstrButtn = "3";
                Log.i(mstrButtn, "I just clicked on");
                break;

                       
            default:
                break;}}}

这就是它的样子:

这就是它的样子

我的 activity_main.xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:background="@android:color/black"
    tools:context=".MainActivity">
<Button
        android:id="@+id/button3"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="3dp"
        android:paddingLeft="0dp"
        android:paddingTop="0dp"
        android:paddingRight="0dp"
        android:paddingBottom="0dp"
        android:text="3"
        android:textSize="24sp"
        android:backgroundTint="#8C8A8A"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/divider3" />
<Button
        android:id="@+id/DigiB3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="70dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:padding="0dp"
        android:paddingLeft="0dp"
        android:paddingTop="0dp"
        android:paddingRight="0dp"
        android:paddingBottom="0dp"
        android:text="3"
        android:textSize="30sp"
        app:backgroundTint="#009688"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/divider3" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginTop="8dp"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/divider6"
        app:layout_constraintVertical_bias="0.0">

    </com.google.android.gms.ads.AdView>

【问题讨论】:

  • 最好添加你的styles.xml或colors.xml和你的layout.xml。
  • 分享您的activity_main.xml 布局文件以便更好地理解。
  • @FahadNasrullah 我刚刚做了,请告诉我,谢谢

标签: java android android-studio


【解决方案1】:

Mb 您在 xml 代码中使用 tools:backgroundTint="@color/yourColor" 吗?如果我是对的,那么只需将工具属性替换为 android,如下所示:

android:backgroundTint="@color/yourColor"

作者添加 XML 部分后更新。

只需将app:backgroundTint="#009688" 替换为android:backgroundTint="#009688"

【讨论】:

  • 您不清楚在哪里进行此更改。我假设在 XML 的 Button 部分。我试过了,它不会接受。
  • 只需将 app:backgroundTint="#009688" 替换为 android:backgroundTint="#009688"
【解决方案2】:

参考这个答案:What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

基本上,BackgroundTint 被应用到背景,因为你没有任何背景属性它什么都不做。

编辑:似乎按钮已经有背景,测试您的 xml 隔离显示按钮具有您想要的颜色,也许是您的适配器中的一些东西?

【讨论】:

    【解决方案3】:

    使用 android:background 代替 android:backgroundTint。 backgroundTint 仅适用于 API >= 21。

    <Button
            android:id="@+id/DigiB3"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginTop="70dp"
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            android:padding="0dp"
            android:paddingLeft="0dp"
            android:paddingTop="0dp"
            android:paddingRight="0dp"
            android:paddingBottom="0dp"
            android:text="3"
            android:textSize="30sp"
            app:background="#009688"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/divider3" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 2019-05-28
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多