Android 开发实战 布局
一、相对布局 RelativeLayout
RelativeLayout 下级视图的位置是相对位置,得有具体的参照物才能确定最终位置,如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout 内部的左上角。用于确定视图位置的参照物分两种,一种是与视图自身平级的视图,另一种是该视图的上级视图(RelativeLayout)
代码
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.kangxg.layout.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:id="@+id/Hellotextview"/> //这里注意版本的区别 <RelativeLayout android:layout_below="@+id/Hellotextview" android:id="@+id/rl_content" android:layout_height="match_parent" android:layout_width="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:id="@+id/btn_center" style="@style/btn_relative" android:text="我在中间" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:id="@+id/btn_center_horizontal" style="@style/btn_relative" android:text="我在水平中间" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:id="@+id/btn_centerVertival" style="@style/btn_relative" android:text="我在垂直中间" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_alignParentLeft="true" android:id="@+id/btn_parent_left" style="@style/btn_relative" android:text="我跟上级左边对齐" /> <Button android:layout_width="120dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:id="@+id/btn_parent_top" style="@style/btn_relative" android:text="我跟上级顶部对齐" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_alignParentRight="true" android:id="@+id/btn_parent_right" style="@style/btn_relative" android:text="我跟上级右边对齐" /> <Button android:layout_width="120dp" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:id="@+id/btn_parent_bottom" style="@style/btn_relative" android:text="我跟上级底部对齐" /> <Button android:id="@+id/btn_left_bottom" style="@style/btn_relative" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/btn_parent_bottom" android:layout_toLeftOf="@+id/btn_parent_bottom" android:text="我在底部左边" /> <Button android:id="@+id/btn_right_bottom" style="@style/btn_relative" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/btn_parent_bottom" android:layout_toRightOf="@+id/btn_parent_bottom" android:text="我在底部右边" /> <Button android:id="@+id/btn_above_center" style="@style/btn_relative" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/btn_center" android:layout_alignLeft="@id/btn_center" android:text="我在中间上边" /> <Button android:id="@+id/btn_blow_center" style="@style/btn_relative" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_center" android:layout_alignRight="@id/btn_center" android:text="我在中间下边" /> </RelativeLayout> </android.support.constraint.ConstraintLayout>
import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; public class MainActivity extends AppCompatActivity implements OnClickListener { private RelativeLayout rl_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rl_content = (RelativeLayout)findViewById(R.id.rl_content); Button centerbtn = (Button)findViewById(R.id.btn_center); centerbtn.setOnClickListener(this); Button hcenterbtn = (Button)findViewById(R.id.btn_center_horizontal); hcenterbtn.setOnClickListener(this); Button vcenterbtn = (Button)findViewById(R.id.btn_centerVertival); vcenterbtn.setOnClickListener(this); Button lcenterbtn = (Button)findViewById(R.id.btn_parent_left); lcenterbtn.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_center) { addNewView(RelativeLayout.CENTER_IN_PARENT,-1,rl_content.getId()); } else if (v.getId() == R.id.btn_center_horizontal) { } else if (v.getId() == R.id.btn_centerVertival) { } else if (v.getId() == R.id.btn_parent_left) { addNewView(RelativeLayout.LEFT_OF,RelativeLayout.ALIGN_TOP,v.getId()); } } private void addNewView(int firstAlign,int SencondAlign,int referId) { View v = new View(this); v.setBackgroundColor(0xaa66ff66); RelativeLayout.LayoutParams rl_params = new RelativeLayout.LayoutParams(100,100); rl_params.addRule(firstAlign,referId); if (SencondAlign>=0) { rl_params.addRule(SencondAlign,referId); } v.setLayoutParams(rl_params); v.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { rl_content.removeView(view); return true; } }); addContentView(v,rl_params); } } 运行效果
二、框架布局 FrameLayout
FrameLayout 也是较常见的布局,其下级视图无法指定所处的位置,只能统统从上级FrameLayout 的左上角开始添加,并且后面添加的子视图会把之前的子视图覆盖掉,框架布局一般用于需要重叠显示的场合,比如绘图、游戏界面等,常见属性说明如下。
foreground: 指定框架布局的前景图像。该图像在框架内部永远处于最顶层,不会被框架内的其他视图覆盖。
foregroundGravity: 指定前景图像的对其方式
三、线性布局 LinerLayout
LinerLayout 是最常见的布局,内部视图的排列排列是有顺序的,要么上下排列要么左右依次排列。
四、绝对布局 AbsoluteLayout
AbsoluteLayout :按照绝对坐标来布局组件
五、表格布局 TableLayout
TableLayout:按照行列方式布局组件
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列android:shrinkColumns: 收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns: 尽量把指定的列填充空白部分
android:layout_column: 控件放在指定的列
android:layout_span: 该控件所跨越的列数
简单的列子:
①效果图:
总结。没有捷径,只要重复去操作,练习到吐,再每一次错误解决后总会有收获!