zoukankan      html  css  js  c++  java
  • Android 控件属性

    TextView 文字属性
    //文字左右居中
    android:layout_centerHorizontal="true"
    //文字垂直居中
    android:layout_centerVertical="true"
    //文字大小
    android:textSize="80px"
    //背景颜色
    android:background="#00FF00"

    //获取页面对象的值用findViewById 来获取

    例子:获取TextView的值

    TextView textView = (TextView)findViewById(R.id.空间ID);

    ----------------------------------------------------

    2、View是所以控件的父类。

    文本、按钮、多选、单选、布局、等等都集成View。

    ----------------------------------------------------

    3、监听器的使用基本流程。

      ①获取代表控件的对象

      ②定义一个类,实现监听器接口

      ③生成监听器对象

      ④为控件绑定监听器对象

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout);
    
               textview = (TextView)findViewById(R.id.txtView);
               textview.setText("第一个TextView");
               textview.setBackgroundColor(Color.GREEN);
               //拿到Button控件
               btn = (Button)findViewById(R.id.btn);
               ButtonListener btns = new ButtonListener();
               btn.setOnClickListener(btns);
    
        }
    
        //监听器类
        class ButtonListener implements OnClickListener
        {
            @Override
            public void onClick(View view) {
                count_S++;
                textview.setText(count_S+"");
            }
        }

    -----------------------------------------------------

    4、布局方法分类(-)

       1、linerLayout 线性布局   

       2、RelativeLayout 相对布局   用的比较多

       3、ListView布局

       4、GridView布局

    -------------------------------------------

    5、距离单位

      1、距离单位px

      2、距离单位dp

      3、距离单位sp

      4、控件的外边距和内边距

      (1)什么是dpi (dots per inch)dpi是屏幕的细腻程度。

      计算公式:

         dpi = (height² + width²)开根号  ,除以 size

      dp = dip(Device Independent pixels)

      换算公式 px = dp *(dpi /160)

      在dpi为160的屏幕上:1dp =1px

        (2)sp 通常用于指定字体大小

         ① sp: scaled pixels

         ② sp单位通常用于指定字体大小

         ③ 当用户修改手机字体是,sp会随之改变

      (3) 字体用sp,控件大小用dp。

     -----------------------------------------------------------------------------

    6、内边距和外边距

      (1)设置内边距和外边距

    7、CheckBox 全选和全不选

      

    private CheckBox eatBox;
        private CheckBox sleepBox;
        private CheckBox dotaBox;
        private CheckBox AllCheck;
        private CheckBox CkLOL;
    
        private TextView textview;
        private Button btn;
        int count_S = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.checkbox);
    
            eatBox = (CheckBox) findViewById(R.id.eatId);
            sleepBox = (CheckBox) findViewById(R.id.sleepId);
            dotaBox = (CheckBox) findViewById(R.id.dotaId);
            AllCheck = (CheckBox)findViewById(R.id.AllCheckId);
            CkLOL = (CheckBox)findViewById(R.id.LOL);
    
            AllCheckListener al = new AllCheckListener();
            AllCheck.setOnCheckedChangeListener(al);
    
        }
    
        class AllCheckListener implements CompoundButton.OnCheckedChangeListener
        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
          //方法一:
    if (isChecked) { eatBox.setChecked(true); sleepBox.setChecked(true); dotaBox.setChecked(true); CkLOL.setChecked(true); System.out.println("All Check"); }else{ eatBox.setChecked(false); sleepBox.setChecked(false); dotaBox.setChecked(false); CkLOL.setChecked(false); System.out.println("unCheck"); }

          ----------
          方法二:
            eatBox.setChecked(isChecked);
            sleepBox.setChecked(isChecked);
            dotaBox.setChecked(isChecked);
            CkLOL.setChecked(isChecked);
            }

     8、单选按钮Radio

      private RadioGroup radioGroup;
        private RadioButton nan;
        private RadioButton nv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.radiobox);
    
            radioGroup = (RadioGroup) findViewById(R.id.radioGroupId);
            nan = (RadioButton)findViewById(R.id.nanID);
            nan.setChecked(true);   //默认是第一个被选中
    
            nv = (RadioButton)findViewById(R.id.nvId);
    
            RadioGroupListener listener = new RadioGroupListener();
            radioGroup.setOnCheckedChangeListener(listener);
    
    
        }
    
        class RadioGroupListener implements RadioGroup.OnCheckedChangeListener
        {
            //id被选中单选单选按钮ID
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id)
            {
                //radioGroup 有多个组情况要先判断是哪个Group
                if (radioGroup.getId() == R.id.radioGroupId)
                {
                    if (id == nan.getId())
                    {
                        System.out.println("选中男");
                    }else if (id==nv.getId())
                    {
                        System.out.println("选中nv");
                    }
                }else{
                    System.out.println("没有选中按钮");
                }
            }
        }

    9、图片控件 ImageView

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="206dp"
            android:id="@+id/imageView"
            android:layout_gravity="center_vertical"
            android:src="@mipmap/a12" />   // 图片路径

    ScaleType 拉伸类型

    10、android:layout_weight="1"

    是平分剩余的空间,而不是把父容器平分

     

    如果想让第一个控件占屏幕的1/3,第二个控件占2/3

      <TextView
            android:id="@+id/firstID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#00FF00"
            android:layout_weight="1"
            android:text="first101010"/>
    
        <TextView
            android:id="@+id/secondID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="#002200"
            android:text="second"/>

    height 也是可以这样用。

    11、相对布局

    <TextView
    android:id="@+id/firstView" //@+id 是创建ID
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:text="Hello World!"
    />
    <TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/firstView" //@id 是引用ID 是存在的ID
    android:background="#00FF00"
    android:text="World Hello!"/>
  • 相关阅读:
    PID控制算法的C语言实现六 抗积分饱和的PID控制算法C语言实现
    PID控制算法的C语言实现五 积分分离的PID控制算法C语言实现
    python获取两个dict的不同
    将nosetests的echo结果保存到本地文件
    django 配置中STATICFILES_DIRS 和STATIC_ROOT不能同时出现
    Windows下使用最新的JDK1.7.0_51以上版本连接Jenkins出现SecurityException
    jenkins 启动slave,出现com.sun.deploy.net.FailedDownloadException: Unable to load resource: http://127.0.0.1:8080/jnlpJars/remoting.jar
    windows 下查看端口占用情况
    phpstorm + x-debug 进行php调试
    jenkins 升级jdk到1.8.0 报java.io.IOException:Unable to read /var/lib/jenkins/config.xml
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/5463787.html
Copyright © 2011-2022 走看看