• 安卓开发FirstDay


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

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

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

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

    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    剩余空间:父元素总高度 - 第一个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*剩余空间
    
    • 1
    • 2
    • 3

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

    android:background="@color/purple_500"
    
    • 1

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    android:layout_weight

    androidx.constraintlayout.widget.ConstraintLayout

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

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

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

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

            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
    
    • 1
    • 2

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

            app:layout_constraintTop_toXXXOf="xxxx"
            app:layout_constraintStart_toXXXOf="xxxx"
    
    • 1
    • 2

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

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
    
    • 1
    • 2
    • 3
    • 4

    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));
                    }
            );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    发送方

    		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);  // 页面跳转
                    }
            );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    接收方

            TextView tvMessage = findViewById(R.id.tvCount);
    
            Intent intent = getIntent();		// 接收传过来的消息
            String message = intent.getStringExtra(MessageActivity.MESSAGE);
    
            if(message != null){
                if(tvMessage != null){
                    tvMessage.setText(message);
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

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

  • 相关阅读:
    基于matlab的光纤通信误码率仿真
    2023测试工程师做哪些准备,才能从众人中脱颖而出,不看后悔10年
    学术团体的机器人相关分会和机器人相关大赛的说明
    PAT 甲级 A1003 Emergency
    vue2实现自定义主题webpack-theme-color-replacer
    图08 --- 网络的最大流
    k8s之有状态服务部署基石(基础知识)
    【LeetCode每日一题】——171.Excel 表列序号
    Windows7 - 永恒之蓝 - 测试
    Android Studio 生成开发库并打包maven包上传到私有服务器使用
  • 原文地址:https://blog.csdn.net/qq_53318060/article/details/125550392