赞
踩
目录
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"?>
- <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"
- tools:context=".DictionaryTableActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:ignore="MissingConstraints">
- <Button
- android: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>
- </androidx.constraintlayout.widget.ConstraintLayout>

-
- public class DictionaryTableActivity extends AppCompatActivity implements View.OnClickListener {
-
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_dictionary_table);
- Button button = (Button) findViewById(R.id.button_2);
- button.setOnClickListener(this);
- }
- @SuppressLint("NonConstantResourceId")
- @Override
- public void onClick(View view) {
- switch (view.getId()){
- case R.id.button_2:
- Toast.makeText(DictionaryTableActivity.this,"Hello world",Toast.LENGTH_LONG).show();
- break;
- default:
- break;
- }
- }
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。