• android入门到熟练(三)----UI界面


    1.TextView

    以下只是一部分属性,还有很多属性需要在用到时候再说

    <TextView

    android:textSize="24sp"//文字大小
    android:textColor="#00ff00"//文字颜色
    android:gravity="center"//排列方向
    android:id="@+id/txtMainOne"
    android:text="这是一个正规的活动界面"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    2.Button

    <Button
    android:id="@+id/btnMainOne"
    android:text="按钮"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    在java代码中给按钮添加事件可以统一处理,方法是让类去实现接口View.OnClickListener

    public class MainActivity extends Activity implements View.OnClickListener {

    //在onCreate方法中绑定按钮的监听事件为类本身

    Button btnMainOne=(Button)findViewById(R.id.btnMainOne);
    btnMainOne.setOnClickListener(this);

    //其他代码。。。。。。以下是对点击按钮的集中处理方式

    @Override
    public void onClick(View v)
    {
    switch (v.getId())
    {
    case R.id.btnMainOne:
    //编写逻辑
    break;
    default:
    break;;
    }
    }

    3.EditText 

    <EditText
    android:id="@+id/txtMainTwo"
    android:hint="起到提示信息的作用"
    android:maxLines="2"//限制文本输入框只有2行,不会因为内容过多而出现控件无限拉长
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    //获取EditText 中的值

    EditText txtMainTwo=(EditText)findViewById(R.id.txtMainTwo);
    String value=txtMainTwo.getText().toString();
    Toast.makeText(MainActivity.this,value,Toast.LENGTH_SHORT).show();

    4.ImageView

    <ImageView
    android:id="@+id/imgMainOne"
    android:src="@drawable/ic_launcher"//图片地址
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    ImageView imageView=(ImageView)findViewById(R.id.imgMainOne);
    imageView.setImageResource(R.drawable.one);//修改图片

    5.ProgressBar显示进度

    <ProgressBar

    android:visibility="visible"//所有空间都有这个熟悉,visible,invisible和gone,分别表示显示空间,隐藏控件但是占用位置,直接删除控件
    android:id="@+id/pgbMainOne"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"//设置为横向表现
    android:background="#ff5b7fff"//背景颜色
    android:max="100"/>//最大值

    代码控制显示

    ProgressBar progressBar=(ProgressBar)findViewById(R.id.pgbMainOne);
    if(progressBar.getVisibility()==View.GONE)
    {
    progressBar.setVisibility(View.VISIBLE);
    }
    else
    {
    progressBar.setVisibility(View.GONE);
    }

    //设置进度条

    int progress=progressBar.getProgress();
    progress+=10;
    progressBar.setProgress(progress);

    6.AlertDialog

    AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.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 which) {

    }
    });
    dialog.show();

    7.ProgressDialog 

     结合多线程展示

    final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
    progressDialog.setTitle("提示");//设置标题
    progressDialog.setCanceledOnTouchOutside(false);//是否在空白处点击返回主界面
    progressDialog.setMax(100);//设置为横向进度条最大值
    progressDialog.setMessage("提示内容信息");//提示内容
    progressDialog.setCancelable(false);//是否选择取消键返回
    progressDialog.setProgress(40);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置为横向进度
    progressDialog.setIcon(R.drawable.one);//设置图标
    progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定",new DialogInterface.OnClickListener() {//添加按钮事件
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(MainActivity.this,"按下按钮",Toast.LENGTH_LONG).show();
    }
    });
    progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
    });
    progressDialog.show();//显示
    new Thread(new Runnable() {//创建多线程
    @Override
    public void run() {
    // TODO Auto-generated method stub
    int i = 0;
    while (i < 100) {
    try {
    Thread.sleep(200);
    // 更新进度条的进度,可以在子线程中更新进度条进度
    progressDialog.incrementProgressBy(1);//设置进度条增加值
    // dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
    i++;

    } catch (Exception e) {
    // TODO: handle exception
    }
    }
    progressDialog.dismiss();//在进度条走完时删除progressDialog

    }
    }).start();//启动多线程

    8.布局:常用的4种布局LinearLayout,RelativeLayout,FrameLayout

    LinearLayout:线性布局方式,android:orientation="vertical"表示竖向排列,android:orientation="horizontal"表示横向排列

    android:layout_gravity="right"//设置控件的位置,当是横向的时候只能设置竖向的位置,当为竖向的时候只能设置横向的位置。

    当设置android:orientation="horizontal"时可以设置控件的宽度是0:android:layout_width="0dp",然后设置宽度上的权重android:layout_weight="1"表示占用的比例

    RelativeLayout:相对位置布局,可以设置对应的属性设置相对于父元素的坐标或者某个控件的坐标

    相对于父元素

    layout_alignParentLeft

    layout_alignParentTop

    layout_alignParentRight

    layout_centerInParent

    layout_alignParentBottom

    相对于某个控件

    android:layout_above="@+id/btnBThree"//相对于某个控件的上方

    android:layout_below="@+id/btnBThree"//相对于某个空间的下方
    android:layout_toLeftOf="@+id/btnBThree"
    android:layout_toRightOf="@+id/btnBThree"
    android:layout_toStartOf="@+id/btnBThree"
    android:layout_toEndOf="@+id/btnBThree"

    android:layout_alignBottom="@+id/btnBThree"
    android:layout_alignTop="@+id/btnBThree"
    android:layout_alignRight="@+id/btnBThree"
    android:layout_alignLeft="@+id/btnBThree"
    android:layout_alignEnd="@+id/btnBThree"
    android:layout_alignStart="@+id/btnBThree"

    FrameLayout:所有的控件都会在左上角定位,会出现重叠现象,不常用,但是在碎片的章节将会出现

    TableLayout:允许以表格的方式排列控件,不常用。

    9.自定义控件

    先设计一个layout文件,然后再别的页面引入已经设计好的界面:<include layout="@layout/title"/>

    如果是自定义控件:先定义一个类继承至LinearLayout,在构造函数中编写该类的事件

    public class TitleLayout extends LinearLayout {

    public TitleLayout(Context context,AttributeSet attrs)
    {
    super(context,attrs);
    LayoutInflater.from(context).inflate(R.layout.title,this);

    Button btnTitleOne=(Button)findViewById(R.id.title_back);
    btnTitleOne.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
    ((Activity)getContext()).finish();//找到当前所在活动
    }
    });

    Button btnTitleTwo=(Button)findViewById(R.id.title_edit);
    btnTitleTwo.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
    Toast.makeText(getContext(),"你选择了编辑按钮",Toast.LENGTH_SHORT).show();
    }
    });
    }
    }

    开始使用该用户控件

    <com.example.zhb.test2.TitleLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></com.example.zhb.test2.TitleLayout>

  • 相关阅读:
    JSONObject和JSONArray区别及基本用法
    MySQL中UTF8编码的数据在cmd下乱码
    js页面刷新跳转的几种方式及区别
    JS中使用EL表达式
    $.ajax()方法参数详解
    Firefox默认英文修改中文
    配置文件的相对地址和绝对地址设置
    IntelliJ IDEA设置统一编码utf-8
    java.lang.SecurityException: Prohibited package name:
    flask的多个url对应同一个视图函数
  • 原文地址:https://www.cnblogs.com/yuliantao/p/4329229.html
Copyright © 2020-2023  润新知