从零开始的安卓开发

安卓开发(持续更新中

Android <-> 接口 <-> 数据库

Java文件过程:

1、声明变量

2、找到对应变量 findViewById(可能需要强制类型转换)

3、添加方法描述

布局

线性布局 LinearLayout,相对布局 RelativeLayout

线性布局 LinearLayout

常用属性
android:idandroid:layout_margin 外边距
android:layout_widthandroid:layout_padding 内边距
android:layout_heightandroid:orientation
android:backgroundandroid:gravity 内部元素对齐方式
android:weight 权重,多元素考虑,父空间剩余平分

一般使用单位为dp而不是px,其目的是为了做到自适应。

相对布局 RelativeLayout

常用属性
android:layout_toLeftOfandroid:layout_alignParentBottom
android:layout_toRightOfandroid:layout_below
android:layout_alignBottom 底部对齐

BUTTON

按钮颜色无法修改:app/res/values/themes

修改<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

通过建立drawable文件自定义按钮外观属性。

常用属性shape常用属性selector (按下变色
stroke 描边 width color state_pressed
solid 填充 color
coners 圆角 radius 四周圆角

点击属性android:onclick="showToast",不常用

public void showToast(View view) {
        Toast.makeText(this, "Btn4 Clicked.", Toast.LENGTH_SHORT).show();
    }

点击操作,直接在java中实现,常用

	mBtn3 = findViewById(R.id.btn_3);
	mBtn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {	//xml未指定方法名,需用ButtonActivity.this来指定上下文
            Toast.makeText(ButtonActivity.this, "Btn3 Clicked.", Toast.LENGTH_SHORT).show();
        }
    });

EditText

常用属性
inputType 指定输入格式 number, textPassword

Java添加输入框内容监听:

        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

需要先行添加RadioGroup,并且在其中添加相应的RadioButton。

    <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="male"
            android:textSize="18sp"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="female"
            android:textSize="18sp"/>
    </RadioGroup>>

**注意:**checked为默认选中,使用时需对每个RadioButton给定id。

CheckBox

ImageButton

Button其他衍生控件:ToggleButton、Switch

添加背景图片android:src="@drawable/im_01"

scaleType常用属性:图片拉伸方式
fitXY 占满控件centerCrop 保持宽高比缩放,占满控件,裁剪显示
fitCenter 保持宽高比缩放,直至能够完全显示

加载网络图片:调用第三方库Glide,目前图片无法显示为解决

已解决:app删了,重新安装即可(我他妈

AndroidManifest需添加android:usesCleartextTraffic="true",且url应当是https。

Gradle按照提示添加代码

repositories {
  google()
  jcenter()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.12.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}

AndroidManifest赋予网络权限

    <uses-permission android:name="android.permission.INTERNET"/>

java

        mIv4 = findViewById(R.id.iv_4);
        Glide.with(this).load("http://goo.gl/gEgYUd").into(mIv4);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值