UI组件之Button
- 文字大小
- 自定义背景形状
- 自定义按压效果
- 点击事件
练习代码
activity_main.xml
<Button android:id="@+id/btn_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button"/>
MainActivity.java
public class MainActivity extends AppCompatActivity { private Button mBtnButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnButton = findViewById(R.id.btn_button); mBtnButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳转到Button演示界面 Intent intent = new Intent(MainActivity.this,ButtonActivity.class); startActivity(intent); } }); } }
activity_button,xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".ButtonActivity" android:padding="15dp"> <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="40dp" android:text="按钮1" android:textSize ="20sp" android:textColor="#FF5722" android:background="#FFEB3B"/> <Button android:id="@+id/btn_2" android:layout_width="match_parent" android:layout_height="40dp" android:text="按钮2" android:textSize="20sp" android:textColor="#000000" android:background="@drawable/bg_btn2" android:layout_below="@+id/btn_1" android:layout_marginTop="15dp"/> <Button android:id="@+id/btn_3" android:layout_width="match_parent" android:layout_height="40dp" android:text="按钮3" android:textSize="20sp" android:textColor="#000000" android:background="@drawable/bg_btn3" android:layout_below="@+id/btn_2" android:layout_marginTop="15dp"/> <Button android:id="@+id/btn_4" android:layout_width="match_parent" android:layout_height="40dp" android:text="按钮4" android:textSize="20sp" android:textColor="#000000" android:background="@drawable/bg_btn4" android:layout_below="@+id/btn_3" android:onClick="showToast" android:layout_marginTop="15dp"/> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="50dp" android:text="哈哈哈哈" android:gravity="center" android:textColor="#9C27B0" android:textSize="30sp" android:layout_below="@+id/btn_4" android:layout_marginTop="15dp" android:background="#FFE4E1"/> </RelativeLayout>
ButtonActivity.java
package com.example.helloworld; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class ButtonActivity extends AppCompatActivity { private Button mBtn3; private Button mBtn2; private TextView mTv1; @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 v) { Toast.makeText(ButtonActivity.this,"btn3被点击了",Toast.LENGTH_SHORT).show(); } }); mBtn2 = findViewById(R.id.btn_2); mBtn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ButtonActivity.this,"btn2被点击了",Toast.LENGTH_SHORT).show(); } }); mTv1 = findViewById(R.id.tv_1); mTv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ButtonActivity.this,"哈哈哈哈被点击了",Toast.LENGTH_SHORT).show(); } }); } public void showToast(View view){ Toast.makeText(this,"btn4被点击了",Toast.LENGTH_SHORT).show(); } }
drawable/bg_btn2.xml:实心圆角
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF9900"/> <corners android:radius="5dp"/> </shape>
空心
<stroke android:width="1dp" android:color="#FF9900"/>
按压效果
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <!--当按钮被按压时实行一些样式--> <shape> <solid android:color="#99FF33"/> <corners android:radius ="5dp" /> </shape> </item> <item android:state_pressed="false"> <shape> <solid android:color="#CCFF99"/> <corners android:radius ="5dp" /> </shape> </item> </selector>