zoukankan      html  css  js  c++  java
  • android界面UI的构造方式

      android的UI组件都在android.widget包及子包里,和android.view包里,

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.TextView;

    ×在XML中构造界面

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_text"
            android:id="@+id/button"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

      button的id和text属性都避免使用硬编码的方式赋值,而是采取android:id="@+id/button",android:text="@string/button_text"的形式,在res—》values,strings.xml中构造,<string name="button_text">Click</string>,有利于代码的维护。

      作为一种构造事件处理器的方式,在属性android:onClick=“”中为控件绑定元素,并在代码中实现。其中onClickButton(View v),v传递一个事件源对象,即产生事件的元素{为什么不是(view v)呢?view是抽象基类,不能实例化的}

    android:onClick="onClickButton" 

    public void onClickButton(View v){
        TextView textview= (TextView)findViewById(R.id.textView);
    textview.setText("clicked !");
    }

    java.lang.Object

       ↳ android.view.View
         ↳ android.widget.TextView
           ↳ android.widget.Button

    ×在代码中构造界面

      利用代码的形式创建界面元素,设置其布局信息和属性。

            LinearLayout liLayout=new LinearLayout(this);
        //this传递一个Context参数,用于获取android应用环境的全局信息
    setContentView(liLayout);
        //设置要显示的窗口,其参数也可以是XML文件,R.Layout.** liLayout.setOrientation(LinearLayout.VERTICAL);
        //
    setOrientation()方法设置Orientation
        liLayout.addView(textView);
       //向布局管理器中添加控件内容
    btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    //设置布局的有关信息,其参数中最好精确指定父容器,如ViewGroup.LayoutParams,而不是单纯用new LayoutPrams()

    ×混合两种方法
      一般将简单的、根layout、稳定的用XML,业务实现较复杂的用代码实现
    imageView浏览图片:
    public class MainDemoActivity extends AppCompatActivity {
    
        int[] images=new int[]{R.drawable.wp_ss_20150618_0001,R.drawable.wp_ss_20150619_0001,R.drawable.wp_ss_20150622_0001,R.drawable.wp_ss_20150630_0001,R.drawable.wp_ss_20150706_0001};
        int current=0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_demo);
            RelativeLayout root=(RelativeLayout)findViewById(R.id.reLayout);
            final ImageView imageView=new ImageView(this);
        //声明为final,如果不,onClick()方法用到imageView时会有提示 root.addView(imageView); imageView.setImageResource(images[current]);
        //显示第一张图片 imageView.setOnClickListener(
    new View.OnClickListener() { @Override public void onClick(View v) { imageView.setImageResource(images[++current % images.length]);
        //任何整数对于5的余数都在0-4,刚好是一个循环 } });     //匿名内部类使用事件监听器 }
    }

    将使用的图片直接复制在appsrcmain esdrawable    目录下;R.drawable.wp_ss_20150618_0001(参看:Android Resource介绍和使用 - 学习Android - 51CTO技术博客

    http://android.blog.51cto.com/268543/302529/)。

  • 相关阅读:
    .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
    使用Visual Studio Code开发.NET Core看这篇就够了
    ASP.NET Core WebApi使用Swagger生成api说明文档看这篇就够了
    asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程
    【半译】扩展shutdown超时设置以保证IHostedService正常关闭
    从零搭建分布式文件系统MinIO比FastDFS要更合适
    用asp.net core结合fastdfs打造分布式文件存储系统
    在ASP.NET Core中创建基于Quartz.NET托管服务轻松实现作业调度
    Nuget多项目批量打包上传服务器的简明教程
    一个新实验:使用gRPC-Web从浏览器调用.NET gRPC服务
  • 原文地址:https://www.cnblogs.com/zhangyanxiang/p/4821907.html
Copyright © 2011-2022 走看看