px(pixels)像素:不同设备显示效果相同
pt(point)长度单位,印刷业使用
dip(device independent pixels)独立设备像素
即dp,控件使用
sp(scaled pixels)放大像素,字体显示
LayoutParams=Layout信息包,封装了Layout的位置、高、宽
package com.example.lauoutparams;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
LinearLayout linearLayout = new LinearLayout(this);
//设置宽高
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
TextView textView = new TextView(this);
textView.setText("我是文本");
textView.setBackgroundColor(0xffff0000);
//px
LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(300,
300);//如果直接设置宽高,就是像素
// textView.setLayoutParams(textLayoutParams);
//
// linearLayout.addView(textView);
linearLayout.addView(textView,textLayoutParams);
// setContentView(linearLayout,layoutParams);
setContentView(linearLayout);
}
}