安卓开发FirstDay

<item name="android:layout_height">match_parent</item>

控件高度占据父元素全部的剩余高度

<item name="android:layout_weight">1</item>

将父控件的剩余空间,按照设置的权重比例,进行再分配。
比如:

<LinearLayout
    android:orientation="vertical">
    
    <Button
        android:layout_height="wrap_content"/>

    <TextView
       	android:layout_height="0dp"
        android:layout_weight=1/>
        
    <Button
        android:layout_height="wrap_content"/>
        
</LinearLayout>

剩余空间:父元素总高度 - 第一个Button的高度(wrap_content) - 0dp - 第二个Button的高度(wrap_content)
layout_weight属性默认为0。所以 三个子控件的 layout_weight 属性之和是 1。

对于第一个 Button,	其所占高度: wrap_content + 0/1*剩余空间
对于TextView,		其所占高度: 0dp + 1/1*剩余空间
对于第二个 Button,	其所占高度: wrap_content + 0/1*剩余空间

由上,总体效果就是:两个Button的高度都是 wrap_content,TextView占据了剩余的全部高度。
在这里插入图片描述在这里插入图片描述

android:background="@color/purple_500"

Button属性不加 backgroud属性的话,控件内部会有间距。

在写这行代码的时候,报了一个错:Variable expected
猜想可能原因,在做 ++运算的时候,其被操作数得是一个可寻址的变量,而不是中间结果(不可寻址的,如常量7,5,2等)。
类比于 C++中的左值和右值。

int countOrigin = ++ Integer.parseInt((String) tvCount.getText());

// 改进
int countOrigin = Integer.parseInt((String) tvCount.getText());
final int op = ++ countOrigin;

android:layout_weight

androidx.constraintlayout.widget.ConstraintLayout

app:layout_constraintStart_toStartOf="parent"	// 控件的左侧 贴着 父元素的左侧
app:layout_constraintTop_toTopOf="parent"		// 控件的顶部 贴着 父元素的顶部

注意只有这样:
[Start / End] to [Start / End]:左右
[Top / Bottom] to [Top / Bottom]:上下
是有效的。

app:layout_constraintStart_toTopOf="parent"		// Start 对 Top 是无效的, 因为一个方向是左右, 一个方向是上下。

注意,在约束布局中,如果只有这样,去给控件设置边距的话,是无效的。

        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"

必须要有约束,否则设置的margin不会生效

        app:layout_constraintTop_toXXXOf="xxxx"
        app:layout_constraintStart_toXXXOf="xxxx"

这个东西是设置为第一个展示的,加了这个的话,activity 标签要设置属性 android:exported

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

Intent-filter的四个属性Action,Category,Extras,Data

设置一些监听事件

        Button btnShowToast = findViewById(R.id.btnShowToast);
        btnShowToast.setOnClickListener(
                // 弹出气泡
                (view) -> Toast.makeText(MainActivity.this,"Hello World", Toast.LENGTH_SHORT)
                        .show()
        );

        final TextView tvCount = findViewById(R.id.tvCount);
        Button btnCount = findViewById(R.id.btnCount);
        btnCount.setOnClickListener(
                (view) -> {
                    int countOrigin = Integer.parseInt((String) tvCount.getText());
                    tvCount.setText(Integer.toString(++ countOrigin));
                }
        );

发送方

		public static final String MESSAGE = "名字";

        Button btnSend = findViewById(R.id.send_message);
        EditText et = findViewById(R.id.message);
        btnSend.setOnClickListener(
                (view) -> {
                    String s = et.getText().toString();
                    
//                    发送消息, 页面跳转
                    Intent intent = new Intent(MessageActivity.this,
                            OtherActivity.class);
                    intent.putExtra(MESSAGE,s);
                    startActivity(intent);  // 页面跳转
                }
        );

接收方

        TextView tvMessage = findViewById(R.id.tvCount);

        Intent intent = getIntent();		// 接收传过来的消息
        String message = intent.getStringExtra(MessageActivity.MESSAGE);

        if(message != null){
            if(tvMessage != null){
                tvMessage.setText(message);
            }
        }

android 虚拟机显示在了界面里面,怎么把模拟器放到到android studio窗口外
在这里插入图片描述
onSaveInstanceState / onRestoreInstanceState
屏幕旋转时数据丢失问题
模拟器进行屏幕旋转,要打开这个才有效,不然进行旋转是无效的。
在这里插入图片描述

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
need to use a Theme.AppCompat theme (or descendant) with this Activity
使用android.support.v4.widget.SwipeRefreshLayout爆红:
1.使用androidx.swiperefreshlayout.widget.SwipeRefreshLayout
2.配置 ‘androidx.swiperefreshlayout:swiperefreshlayout:1.0.0’ 插件
安卓报错android.support.v4.widget.SwipeRefreshLayout
修改项目名称和包名方法:
打开项目根目录下的setting.gradle文件
Android Studio修改项目名称和包名方法
cannot resolve symbol ‘R’:
实在不行,清理下缓存,重启试试。
cannot resolve symbol ‘R’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值