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/)。

  • 相关阅读:
    POJ 3254 Corn Fields
    【Android】手机号码获取问题
    iOS自动自动隐藏软键盘
    cocos2d-x教程1 hello world
    ORA-02396: exceeded maximum idle time, please connect again的原因
    [置顶] WebService调用工具(AXIS2)
    经典union的使用
    Android Developers:按需求加载视图
    android手机关于google play商店闪退的解决办法
    [置顶] 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
  • 原文地址:https://www.cnblogs.com/zhangyanxiang/p/4821907.html
Copyright © 2011-2022 走看看