目录
一、Button是什么?
二、使用步骤
1.demo
Button控件具有许多属性可以用来自定义其外观和行为。以下是Button控件常用的参数属性及其详解:
android:id
:设置Button的唯一标识符。在代码中可以使用这个ID引用Button。
android:layout_width
:指定Button的宽度,可以使用以下值:
android:layout_height
:指定Button的高度,可以使用以下值:
android:text
:设置Button显示的文本内容。
android:textColor
:设置Button文本的颜色。
android:textSize
:设置Button文本的大小。
android:background
:设置Button的背景,可以使用以下值:
android:padding
:设置Button的内容与边框之间的内边距,可以使用以下值:
android:gravity
:设置Button中文本内容的对齐方式。
android:onClick
:设置Button点击事件的回调方法。
这些是常用的。
<?xml version="1.0" encoding="utf-8"?> &straintlayout.widget.ConstraintLayout xmlns:android=""xmlns:app=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".DictionaryTableActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:ignore="MissingConstraints"><Buttonandroid:id="@+id/button_2"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="button hello world"tools:ignore="MissingConstraints" /></LinearLayout> </straintlayout.widget.ConstraintLayout>
public class DictionaryTableActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {Create(savedInstanceState);setContentView(R.layout.activity_dictionary_table);Button button = (Button) findViewById(R.id.button_2);button.setOnClickListener(this);}@SuppressLint("NonConstantResourceId")@Overridepublic void onClick(View view) {switch (Id()){case R.id.button_2:Toast.makeText(DictionaryTableActivity.this,"Hello world",Toast.LENGTH_LONG).show();break;default:break;}}
本文发布于:2024-02-02 04:15:57,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681855841293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |