Android学习--基本的控件
前言:
这篇把自己今天看过的控件基本的敲了一遍,后序打算研究一下他们的基本的自定义,代码给出来,方便后买自己再学习看看,熟练了应该也就用不着了:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mxsm.uiwidgettest.MainActivity">
// match_parent 表示当前控件的大小和父控件大小是一样的
// wrap_content 表示当前控件的大小要适配内容
// textSize 字体的大小
// gravity 内容的对齐方式
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Hello World!"-->
<!--android:textSize="24sp"-->
<!--android:gravity="center"-->
<!--/>-->
// 注意这个textAllCaps属性,禁止你写的Button按钮的字母被转成大写
// background 这个属性可以设置图片也可以设置颜色
// drawableLeft 设置左边图片,右边的类推
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="我是一个按钮"
android:background="@color/colorPrimary"
android:drawableLeft="@color/colorAccent"
android:id="@+id/Image_button"
/>
// EditText这个控件 hind属性是设置一点提示文字,其他的基本属性类似
// maxLines 文本输入的最大行数,要是超过这个行数,就会向上滚动
<!--<EditText-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:hint="这是一个输入框"-->
<!--android:maxLines="2"-->
<!--/>-->
<!--// src 指定图片的路径-->
<!--// visibility 设置视图的可见或者不可见,有三个值分别是 gone invisible visible-->
<!--// invisible 表示视图不可见,其实就和设置透明效果了一样,但还是存在的-->
<!--// gone 也是不可见,不过它是不在占用任何的屏幕空间-->
<!--<ImageView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:src="@mipmap/ic_launcher"-->
<!--/>-->
<!--// 注意下面的Style是把它指定成水平的进度条-->
<!--<ProgressBar-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--style="?android:attr/progressBarStyleHorizontal"-->
<!--android:max="100"-->
<!--/>-->
// AlertDialog 提示框 dialog英文是对话框的意思
// ProgressDialog 带加载提示的提示框
// 以上总结的控件
</RelativeLayout>
上面是XMl的布局文件,下面是活动的代码:
package com.example.mxsm.uiwidgettest;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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.Image_button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Image_button:
// setPositiveButton 这个方法添加一个确定按钮
// setNegativeButton 这个方法添加一个取消按钮
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("这是一个弹出框");
alertDialog.setMessage("不是我说,你长的真的挺丑的!");
// 这句话的意思是点击返回键是否能取消掉提示框
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
break;
default:
break;
}
}
}
## 努力做一个合格的程序员。

浙公网安备 33010602011771号