共计 4824 个字符,预计需要花费 13 分钟才能阅读完成。
3.2.1 TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent" #控件宽度
android:layout_height="wrap_content" #控件高度
android:gravity="center" #内容对齐形式
android:textSize="24sp" #字体大小,单位 sp
android:textColor="#00ff00" #字体色彩
android:text="This is TextView" /> #显示的内容
layout_width 和 wrap_content 的可选值:
- match_parent 指定控件大小与父布局一样。
- wrap_content 指定控件大小由控件内容决定。
gravity 的可选值:
- top 顶部对齐
- bottom 底部对齐
- left 右边对齐
- right 左边对齐
- center 等同于 center_vertical|center_horizontal
3.2.2 Button
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false" /> #不主动进行大写转换
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {switch (v.getId()) {
case R.id.button:
// 按钮点击解决逻辑
break;
default:
break;
}
}
}
3.2.3 EditText
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here" #提示性内容
android:maxLines="2" #最大可见行数
/>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
// 获取 EditText 对象
editText = (EditText) findViewById(R.id.edit_text);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {switch (v.getId()) {
case R.id.button:
// 获取 EditText 内容
String input = editText.getText().toString();
Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
3.2.4 ImageView
app/src/main/res/drawable-xhdpi/img_1.png
app/src/main/res/drawable-xhdpi/img_2.png
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img_1"
/>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {switch (v.getId()) {
case R.id.button:
// 更换图片
imageView.setImageResource(R.drawable.img_2);
break;
default:
break;
}
}
}
3.2.5 ProcessBar
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {switch (v.getId()) {
case R.id.button:
if (progressBar.getVisibility() == View.GONE) {
// 显示控件
progressBar.setVisibility(View.VISIBLE);
} else {
// 暗藏控件
progressBar.setVisibility(View.GONE);
}
break;
default:
break;
}
}
}
程度进度条:
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" #设为程度进度条,默认圆形
android:max="100" #最大值为 100
/>
@Override
public void onClick(View v) {switch (v.getId()) {
case R.id.button:
int progress = progressBar.getProgress();
// 进度 +10
progress = progress + 10;
progressBar.setProgress(progress);
break;
default:
break;
}
}
3.2.6 AlertDialog
@Override
public void onClick(View v) {switch (v.getId()) {AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is a dialog");
dialog.setMessage("something important.");
dialog.setCancelable(false);
// 确定按钮的点击事件
dialog.setPositiveButton("OK", new DiaLogInterface.OnClickListener() {
@Override
public void onClick(DiaLogInterface dialog, int whick) {}});
// 勾销按钮的点击事件
dialog.setNegativeButton("Cancel", new DiaLogInterface.OnClickListener() {
@Override
public void onClick(DiaLogInterface dialog, int whick) {}});
// 显示 dialog
dialog.show();
break;
default:
break;
}
}
3.2.7 ProgressDialog
@Override
public void onClick(View v) {switch (v.getId()) {
case R.id.button:
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("This is ProgressDialog");
progressDialog.setMessage("Loading...");
// 不能通过返回键勾销,留神要在程序中解决敞开
progressDialog.setCancelable(false);
progressDialog.show();
break;
default:
break;
}
}
正文完