zoukankan      html  css  js  c++  java
  • 一、Android布局和常用控件

    一、Android布局和常用控件

    1、Android布局

    1.1、线性布局

    1.2、相对布局

    控件位置属性

    1.3、帧布局

    1.4、表格布局

    1.5、绝对布局

    2、常用控件

    2.1、TextView:

    android:text="文本"

    android:textSize="20sp"

    android:textColor="#FF0"

    android:textStyle="bold"

    android:lines="3"

    android:singleLine="true"

    android:typeface="monospace" //设置字型。字形有:normal, sans, serif,monospace

    android:clickable=””

    示例:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="12"
            android:inputType="none"
            android:hint="请输入姓名:"
            android:maxLength="2"
            android:textColor="#000000"
            android:textSize="25sp"
            android:textStyle="italic"
            />
    </LinearLayout>
    

    效果:

    2.2、EditText:

    EditText继承自TextView,可进行编辑操作

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="12"
        android:inputType="none"
        android:hint="请输入姓名:"
        android:maxLength="2"
        android:textColor="#000000"
        android:textSize="25sp"
        android:textStyle="italic"
        />
    

    效果:

    2.3、Button:

    属性与TextView基本相似。

    不同点:

    1. 按钮是自带了背景的控件
    2. 按钮是可以点击了

    Button处理事件的方式:

    1、onClick方式

    2、匿名内部类方式

    3、接口方式

    示例:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/tv_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="25sp"
            android:text="按钮被点击了吗?" />
    
        <Button
            android:id="@+id/button10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="OnClick点击方式" />
    
        <Button
            android:id="@+id/btn_anonymous"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="匿名内部类方式" />
    
        <Button
            android:id="@+id/btn_interface"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="接口方式" />
    
    </LinearLayout>
    
    public class ButtonActivity extends AppCompatActivity implements View.OnClickListener{
    //方式三↑(extends AppCompatActivity implements)
        private TextView tv_show;
        private Button btn_anonymous;       /*方式二*/
        private Button btn_interface;       /*方式三*/
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_button);
            // 获取id
            tv_show = (TextView) findViewById(R.id.tv_show);                 /*方式一、方式二、方式三*/
            btn_anonymous = (Button) findViewById(R.id.btn_anonymous);      /*方式二*/
            btn_interface = (Button) findViewById(R.id.btn_interface);      /*方式三*/
    
            // 匿名内部类监听方式二
            btn_anonymous.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tv_show.setText("匿名内部类处理事件");
                }
            });
    
            //方式三
            btn_interface.setOnClickListener(this);
        }
    
        // onClick事件方式一
        public void click(View v){
            tv_show.setText("onClick方式处理事件");
        }
    
        //接口方式处理事件方式三
        @Override
        public void onClick(View v) {
            tv_show.setText("接口方式处理事件");
        }
    }
    

    效果:

    2.4、RadioButton:

    <RadioGroup /> 单选组

    <RadioButton /> 单选框

    设置监听器的时候,通过 RadioGroup radioGroup.setOnCheckedChangeListener();

    android:checked="true"

    设置监听器:

    示例:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RadioGroup
            android:id="@+id/rdg_gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <RadioButton
                android:id="@+id/rbtn_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="男" />
    
            <RadioButton
                android:id="@+id/rbtn_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="女" />
        </RadioGroup>
    
        <TextView
            android:id="@+id/tv_gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:text="性别是?" />
    </LinearLayout>
    
    public class RadiobuttonActivity extends AppCompatActivity {
    
        private RadioGroup rdg_gender;
        private TextView tv_genter;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_radiobutton);
    
            rdg_gender = (RadioGroup) findViewById(R.id.rdg_gender);
            tv_genter = (TextView) findViewById(R.id.tv_gender);
    
            rdg_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                    if(checkedId==R.id.rbtn_male){
                        tv_genter.setText("您的性别是:男");
                    }else{
                        tv_genter.setText("您的性别是:女");
                    }
                }
            });
    
    
        }
    }
    

    效果:

    2.5、imageView:

    设置图片源

    android:src="@drawable/ic_launcher" 图片大小不会拉伸

    android:background="@drawable/bg" 图片拉伸作为背景

    代码设置图片源

    imageView.setImageResource(resId)

    imageView.setImageBitmap(bm);

    imageView.setImageDrawable(drawable);

    设置缩放模式

    android:scaleType=""

  • 相关阅读:
    欧拉公式
    isap的一些想法
    错误合集
    Hello World
    PAT (Advanced Level) Practice 1068 Find More Coins
    PAT (Advanced Level) 1087 All Roads Lead to Rome
    PAT (Advanced Level) 1075 PAT Judge
    PAT (Advanced Level) 1067 Sort with Swap(0, i)
    PAT (Advanced Level) 1017 Queueing at Bank
    PAT (Advanced Level) 1025 PAT Ranking
  • 原文地址:https://www.cnblogs.com/hign/p/12930765.html
Copyright © 2011-2022 走看看