安装略…
1.android 常用单位
- px=像素
- dp=虚拟像素,在不同的像素密度的设备上会自动适配,推荐使用
- sp=同dp相似,主要用来显示文字,还会根据用户的字体大小偏好来缩放
2.match_parent、fill_parent、wrap_content区别
Android中所有的控件都具有这两个属性,可选值有3种:match_parent、fill_parent、wrap_content.
其中match_parent和fill_parent的意义相同,但官方更推荐match_parent.
match_parent表示让当前控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小
wrap_content表示让当前的控件大小能够刚好包含里面的内容,也就是由控件内容决定当前控件的大小
3.相对布局子控件属性
//相对于同级控件对齐方式
android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐;
android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐;
android:layout_alignBottom 将该控件的底部边缘与给定ID的底部边缘对齐;
android:layout_alignLeft 将该控件的左边缘与给定ID的左边缘对齐;
android:layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐;
// 相对于父组件对齐方式
android:layout_alignParentTop 如果为true,将该控件的顶部与其父控件的顶部对齐;
android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;
android:layout_alignParentLeft 如果为true,将该控件的左部与其父控件的左部对齐;
android:layout_alignParentRight 如果为true,将该控件的右部与其父控件的右部对齐;
// 居中
android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;
android:layout_centerVertical 如果为true,将该控件的置于垂直居中;
android:layout_centerInParent 如果为true,将该控件的置于父控件的中央;
// 控件离上下左右的像素距离
android:layout_marginTop 上偏移的值;
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft 左偏移的值;
android:layout_marginRight 右偏移的值;
//控件相对同级控件的位置
android:layout_toLeftOf 在ID控件的左边
android:layout_toRightOf 在ID控件的右边
android:layout_below 在ID控件的下边
android:layout_above 在ID控件的上边
4.页面跳转+页面传参+接收到参数后显示到页面
主页
package com.example.myapplicationdemo1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn= findViewById(R.id.lineLaout);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 跳转方式一
// Intent i = new Intent(当前类,跳转到的指定类);
// Intent i = new Intent(MainActivity.this , test.class);
// 跳转方式二
Intent i=new Intent();
// intent.setClass(当前类,跳转到的指定类);
i.setClass(MainActivity.this,test.class);
//携带参数跳转(2种)
//----------1
i.putExtra("name","张三");
//----------2
Bundle bundle=new Bundle();
bundle.putString("name2","王五");
bundle.putInt("id",12);
i.putExtras(bundle);
startActivity(i);
}
});
}
public void select(View view){
Toast.makeText(this,"按钮被点击啦",Toast.LENGTH_LONG).show();
}
}
跳转页test
package com.example.myapplicationdemo1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class test extends AppCompatActivity {
private String data;
private Integer id;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Intent i=this.getIntent();
Bundle bundle=this.getIntent().getExtras();
data=i.getStringExtra("name");
name=bundle.getString("name2");
id=bundle.getInt("id");
TextView textView=findViewById(R.id.text1);
textView.setText("我的名字叫:"+data+",id是:"+id);//更新到页面
System.out.println("===================name>"+data);
System.out.println("===================name>"+name+","+id);
}
}