控件,这东西我感觉是最重要的东西,我还记得以前学C的时候最在的想法就是写个界面,那时候窗口就是一个cmd窗口,完全不知道怎么做一个界面,后来学用QT写C++的时候才知道有控件这回事但好像不是很多,后来用VS时,写c++,c#,拖控件拖得好爽。android里面也有很多的控件,但大多数人好像都喜欢用代码来搞。我想先学几个常用的玩玩吧,太高深的用到的再慢慢研究。
常用的属性:
android:id
android:layout_height 高度
android:layout_width 宽度
android:gravity 对齐方式
android:orientation 布局
常用函数 :
findViewById: 如button = (Button)findViewById(R.id.button);
TextView:
最先接触的叫hello world程序
Button:按键
要用到的监听 button.setOnClickListener(new OnClikListernr(){@Override public void onClick(View v){} })
匿名 监听 button.setOnClickListener(this) @override public void onClick(View v){ switck(v.getId()) case R.id.button break;}
EditText:
常用属性:maxlines
常用函数:getText(); setText(); 如String intputText = editText.getText().toString();
ImageView图片:
常用属性src (把资源图片放入drawble 然后build才会在目录中显现出来)
常用函数:setImageResource()
ProgressBar进度条:
常用属性:style="android:attr/progressBarStyleHorizontal"
常用函数:getVisibility() setVisibility() View.VISIBLE View.GONE 可见状态 getProgress(),setProgress()设置、获取进度
AlterDialog单出对话框:
使用方法
AlertDialog.Builder dialog = new AlertDialog.Builder(FirstActivity.this); dialog.setTitle("弹出消息框"); dialog.setMessage("通知信息"); dialog.setCancelable(false); dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int whick){ } }); dialog.show();
ProgressDialog:与AlertDialog相像
ProgressDialog progressDialog = new ProgressDialog(FirstActivity.this); progressDialog.setTitle("有个进度条哦"); progressDialog.setMessage("Loading..."); progressDialog.setCancelable(true); progressDialog.show();
引用布局:
引用布局来解决布局代码重复的问题,说白了就的布局里面布一个布好的局嘛。
我们如果有个title.xml文件如果我们使用了话包进去就好了
<include layout="@layout/title"></include>
创建自定义控件:
创建自定义控件可解决注册代码重复的问题
public class TitleLayout extends LinearLayout { public TitleLayout(Context context, AttributeSet attrs){ super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title, this); } }添加的时候就当普通控件用
<com.ouyang.uicustomviews.TitleLayout>adroid:layout_width="wrap_content" android:layout_height="wrap_content" </com.ouyang.uicustomviews.TitleLayout>