• android基础知识复习一


    android基础

    布局

    线性布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:id="@+id/ll_1"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:orientation="vertical"
            android:background="#000000"
            android:padding="20dp"
           布局 android:layout_marginBottom="10dp">
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0033"></View>
        </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:orientation="horizontal"
        android:background="#ff2673"
        android:layout_marginTop="10dp"
        >
    
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#2236ff"
            android:layout_weight="1"/>
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:layout_weight="1"/>
    </LinearLayout>
    </LinearLayout>
    

    相对布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <View android:id="@+id/view_1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#000000"
            />
        <View android:id="@+id/view_2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#1730B9"
            android:layout_below="@+id/view_1"/>
        <LinearLayout
            android:id="@+id/ll_1"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/view_2"
            android:background="#000000"
            android:orientation="horizontal"
            android:padding="15dp">
            <View
                android:id="@+id/view_3"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#ff2546"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:background="#256336">
                <View
                    android:id="@+id/view_4"
                    android:layout_width="100dp"
                    android:layout_height="match_parent"
                    android:background="#ff2587"
                    />
                <View
                    android:id="@+id/view_5"
                    android:layout_width="100dp"
                    android:layout_height="match_parent"
                    android:background="#ff4536" 
                    android:layout_toRightOf="@+id/view_4"
                    android:layout_marginLeft="10dp"/>
            </RelativeLayout>
    
        </LinearLayout>
        
    </RelativeLayout>
    

    混合使用

    文本

    TextView

    大致流程

    • xml设置布局,+id
    • Activity定义变量,oncreate中通过id获取变量名 R.id.xx
    • 调用相应方法实现功能

    以跳转为例,新建Activity,通过Intent对象进行跳转设置

    start属性进行跳转

    文本设置

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".TextViewActivity"
        android:orientation="vertical"
        android:paddingTop="20dp">
    
        <TextView
            android:id="@+id/tv_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv_test1"
            android:textColor="@color/colorAccent"
            android:textSize="24sp"/>
    
        <TextView
            android:id="@+id/tv_2"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/tv_test1"
            android:maxLines="1"
            android:ellipsize="end"
            android:textColor="@color/colorAccent"
            android:textSize="24sp"
            android:layout_marginTop="10dp"/>
    
    
        <TextView
            android:id="@+id/tv_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大帅的第三次尝试"
            android:layout_marginTop="10dp"
            android:textColor="@color/colorAccent"
            android:textSize="24sp"/>
    
    
    </LinearLayout>
    

    通过id,对资源文件res文件中values的名称进行调用,drawabe文件中图片进行调用@文件夹名称/id

    对于文本自身的下划线等操作,在Activity的oncreate函数中通过对象获取进行设置

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        mTv_1=findViewById(R.id.tv_3);
        mTv_1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中划线
        mTv_1.getPaint().setAntiAlias(true);//除去锯齿
    }
    

    注:在通过id获取变量后可通过html进行特殊设置,且新增的html文本内容取代原来文本。

    mTv_2.setText(Html.fromHtml("<u></u>"));
    

    滚动条设置

    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true"
    

    Button

    1. 通过id获取,调用方法实现点击
    mBtn4=findViewById(R.id.btn_4);
    mBtn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
        }
    });
    
    1. 通过属性绑定方法名,进行点击
    <Button
        android:id="@+id/btn_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮4"
        android:textSize="20sp"
        android:textColor="#123654"
        android:background="@drawable/bg_btn4"
        android:layout_below="@id/btn_3"
        android:onClick="showToast"   绑定语句
        android:layout_marginTop="10dp"/>
    

    按钮的按下效果设置

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#FF9900"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
        <item android:state_pressed="false">
        <shape>
            <solid android:color="#FF3054"/>
            <corners android:radius="10dp"/>
        </shape>
        </item>
    </selector>
    

    res资源文件中drawable中设置xml配置文件,调用selector标签,通过@draw进行调用。

    EditText

    实现简易登录界面

    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="16sp"
        android:textColor="#ff8542"
        android:hint="用户名"/>
    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="16sp"
        android:textColor="#ff8542"
        android:layout_below="@+id/et_1"
        android:inputType="textPassword"
        android:hint="密码"/>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/et_2"
        android:layout_marginTop="40dp"
        android:text="登录"
        />
    

    通过inputType对输入格式进行限制

    监听事件:点击监听无变更,值变化监听

    mEtUserName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("edittext",s.toString());
        }
        @Override
        public void afterTextChanged(Editable s) {
    
        }
    });
    

    RadioButton

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".RadioButtonActivity"
        android:padding="15dp">
        <RadioGroup
            android:id="@+id/rg_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton
                android:id="@+id/rb_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:textSize="24sp"
                android:textColor="#FF6600"/>
            <RadioButton
                android:id="@+id/rb_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"
                android:textSize="24sp"
                android:textColor="#FF6600"
                android:layout_below="@+id/rb_1"
                android:layout_marginTop="5dp"/>
        </RadioGroup>
    
        <RadioGroup
            android:id="@+id/rg_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@+id/rg_1"
            android:layout_marginTop="50dp">
            <RadioButton
                android:id="@+id/rb_3"
                android:layout_width="60dp"
                android:layout_height="30dp"
                android:text="男"
                android:gravity="center"
                android:background="@drawable/selector_orange"
                android:button="@null"
                android:checked="true"
                android:textSize="24sp"
                android:textColor="#000000"
                />
            <RadioButton
                android:id="@+id/rb_4"
                android:layout_width="60dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:text="女"
                android:textSize="24sp"
                android:button="@null"
                android:textColor="#006600"
                android:background="@drawable/selector_orange"
                android:layout_below="@+id/rb_3"
                android:layout_marginLeft="5dp"/>
        </RadioGroup>
    </RelativeLayout>
    

    check属性为默认初始值 button去掉原单选框,可通过xml自定义样式,效果如下图

    image-20210418125604119

    Radio的监听值事件

    mRg1=findViewById(R.id.rg_1);
    mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton=group.findViewById(checkedId);
            Toast.makeText(RadioButtonActivity.this,radioButton.getText(),Toast.LENGTH_SHORT).show();
        }
    });
    

    CheckBox

    用法与上区别不大

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".CheckBoxActivity"
        android:padding="15dp">
    
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你会哪些移动开发"
            android:textColor="#FF0000"
            android:textSize="24dp"/>
        <CheckBox
            android:id="@+id/cb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android"
            android:textSize="18sp"
            android:textColor="#ff6600"
            android:layout_below="@+id/tv_title"
            android:layout_marginTop="15dp"/>
        <CheckBox
            android:id="@+id/cb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ios"
            android:textSize="18sp"
            android:textColor="#ff6600"
            android:layout_below="@+id/cb_1"
            android:layout_marginTop="10dp"/>
        <CheckBox
            android:id="@+id/cb_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="H5"
            android:textSize="18sp"
            android:textColor="#ff6600"
            android:layout_below="@+id/cb_2"
            android:layout_marginTop="10dp"/>
        <CheckBox
            android:id="@+id/cb_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其他"
            android:textSize="18sp"
            android:textColor="#ff6600"
            android:layout_below="@+id/cb_3"
            android:layout_marginTop="10dp"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_below="@+id/cb_4"
            android:layout_marginTop="15dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="你的兴趣:"
                android:textSize="24sp"
                android:textColor="#FF6600"/>
            <CheckBox
                android:id="@+id/cb_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打游戏"
                android:background="@drawable/selector_orange_radiobutton"
                android:textColor="#FF0000"
                android:button="@null"
                android:checked="true"
                android:textSize="18sp"
                android:layout_marginTop="10dp"/>
            <CheckBox
                android:id="@+id/cb_6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="看书"
                android:textColor="#FF0000"
                android:textSize="18sp"
                android:background="@drawable/selector_orange_radiobutton"
                android:checked="true"
                android:button="@null"
                android:layout_marginTop="10dp"/>
            <CheckBox
                android:id="@+id/cb_7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="编程"
                android:textColor="#FF0000"
                android:textSize="18sp"
                android:layout_marginTop="10dp"/>
            <CheckBox
                android:id="@+id/cb_8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="听音乐"
                android:textColor="#FF0000"
                android:textSize="18sp"
                android:layout_marginTop="10dp"/>
        </LinearLayout>
    </RelativeLayout>
    

    注意监听事件的变化

    mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(CheckBoxActivity.this,isChecked?"5选中":"5未选中",Toast.LENGTH_SHORT).show();
        }
    });
    mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(CheckBoxActivity.this,isChecked?"6选中":"6未选中",Toast.LENGTH_SHORT).show();
        }
    });
    

    ImageView

    scaleType:常用的三个属性fitXY、fitCenter、centerCrop

    1. 撑满控件,宽高比可能发生变化
    2. 保持宽高比缩放,直至能够完全显示
    3. 保持宽高比缩放,直至能完全覆盖控件,裁剪显示

    ![img](file:///D:QQ数据2506236179FileRecvMobileFileScreenshot_20210418_141016.jpg)

    效果如图

    加载网络图片

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);
        mIv4=findViewById(R.id.iv_2);
        Glide.with(this).load("http://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").into(mIv4);
    }
    

    .gradle配置语句

    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
    
  • 相关阅读:
    Hibernate中日期(Date)类型 与oracle中日期的映射处理
    mssql注入经常使用的命令
    EL表达式遍历Map集合
    TomCat项目空间映射 配置
    JSP探针虚拟主机JSP环境检测工具
    什么是RSS
    大U盘容量的烦恼:copy 4G 以上单个文件时总是显示U盘已满
    Java初体验之bool
    何为Cookie
    转测试网站各项性能的31 个免费在线工具
  • 原文地址:https://www.cnblogs.com/2506236179zhw/p/14706931.html
Copyright © 2020-2023  润新知