我的代码一定有问题,它将按钮的背景色更改为灰色,并带有黑色文本,并且在运行应用程序进行测试时更改了按钮的大小。我不能指出哪里出了问题。请帮忙。这可能是我的代码和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\u 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>
3条答案
按热度按时间pqwbnv8z1#
你使用的mb
tools:backgroundTint="@color/yourColor"
在xml代码中?如果我是对的,那么就用android替换tools属性,如下所示:android:backgroundTint="@color/yourColor"
在作者添加xml部分后更新。只要替换一下
app:backgroundTint="#009688"
至android:backgroundTint="#009688"
kpbwa7wx2#
请参考以下答案:android布局xml中background、backgroundtint、backgroundtintmode属性之间有什么区别?
基本上,backgroundtint应用于背景,因为你没有任何背景属性,所以它什么也不做。
编辑:似乎按钮已经有了背景,测试xml文件时,会显示带有所需颜色的按钮,可能是适配器中的某个颜色?
pcrecxhr3#
使用android:background instead 的android:backgroundtint. backgroundtint仅适用于api>=21。