Android学习day04【Button】
出现的一些小状况:
小状况
报错,代码显示运行成功
但是无法在模拟机上显示
原因是没有在包含应有id
其二是关于设置背景颜色中
关于background与backgroundTint的区别
//这是backgroundTint的源码 <!-- Tint to apply to the background. --> <attr name="backgroundTint" format="color" /> //这是background的源码 <!-- A drawable to use as the background. This can be either a reference to a full drawable resource (such as a PNG image, 9-patch, XML state list description, etc), or a solid color such as "#ff000000" (black). --> <attr name="background" format="reference|color" />
- 其实这个注释已经说了backgroundTint是应用到背景上的色彩
- 而background是一个可绘制的背景,可以是一个完全可绘制资源的引用(例如图片、可调整大小位图9-patch、XML状态列表描述、etc),或者是纯色如黑色。
自然就是,如果你只是要给背景上纯色的话,建议用backgroundTint,如果你要用背景图片的话,就用background,可以分别提高对应的执行效率!!!
其三
关于Android drawable和drawable-v24文件夹有什么区别
’
当我们放入图片在drawable-v24文件夹时,在该文件夹引用该图片时会导致模拟器无法运行
经常我们放置图片一般默认drawable,mipmap-hdpi,mipmap-mdpi,mipmap-xhdpi文件夹下,这样的情况下我们运行起来的APP也不会出现什么问题。那么如果图片放在drawable-v24,mipmap-anydpi-v26文件夹下就会出现问题。
两个文件夹的区别是:用于为设备兼容性和不同的Android版本提供不同的屏幕密度
所以解决办法就是把图片放在drawable文件夹中这样模拟器就会正常运行了。
使用Button标签的相关效果展示及其代码
button标签可以实现界面跳转、描边、圆角、提示信息(toast)
实现效果
部分代码
(点击信息代码.java文件)

package com.example.app02; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class ButtonActivity extends AppCompatActivity { private Button mBtn3; private TextView tv1; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button); mBtn3=findViewById(R.id.btn_3); mBtn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(ButtonActivity.this, "btn3被点击了!", Toast.LENGTH_SHORT).show(); } }); tv1=findViewById(R.id.tv_1); tv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(ButtonActivity.this, "tv1被点击了", Toast.LENGTH_SHORT).show();; } }); } public void showToast(View view){ Toast.makeText(this, "btn4被点击了!", Toast.LENGTH_SHORT).show(); } }
(相关标签.xml文件)

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="15dp"> <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="50dp" android:text="按钮1" android:textSize="20sp" android:textColor="#FFFFFF" android:backgroundTint="#FF0000"/> <Button android:id="@+id/btn_2" android:layout_width="match_parent" android:layout_height="50dp" android:text="按钮2" android:textSize="20sp" android:textColor="#FFFFFF" android:background="@drawable/bg_btn2" android:layout_marginTop="10dp" android:layout_below="@+id/btn_1"/> <Button android:id="@+id/btn_3" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/btn_2" android:text="按钮3" android:textSize="20sp" android:textColor="#FF9900" android:layout_marginTop="10dp" android:background="@drawable/bg_btn3"/> <Button android:id="@+id/btn_4" android:layout_width="match_parent" android:layout_height="50dp" android:text="按钮4" android:textSize="20sp" android:textColor="#FFFFFF" android:layout_below="@id/btn_3" android:onClick="showToast" android:background="@drawable/bg_btn4" android:layout_marginTop="10dp"/> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="50dp" android:textColor="#000000" android:text="文字1" android:textSize="20sp" android:layout_below="@id/btn_4" android:gravity="center" android:background="#FF9900" android:layout_marginTop="10dp"/> </Rel
(跳转界面.java文件)

package com.example.app02; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { //先声明空间 private Button mBtnTextView; private Button mBtnButton; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnTextView = findViewById(R.id.btn_textview); mBtnTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 跳转到TextView演示界面 Intent intent = new Intent(MainActivity.this, TextViewActivity.class); startActivity(intent); } }); mBtnButton= findViewById(R.id.btn_button); mBtnButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 跳转到Button演示界面 Intent intent=new Intent(MainActivity.this,ButtonActivity.class); startActivity(intent); } }); } }
本文作者:TranquilTimber
本文链接:https://www.cnblogs.com/gbrr/p/17027648.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
Android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步