202302150749
布局
线性布局
线性布局会将所有子视图按垂直或水平的顺序排列。
用于需要简单、按顺序排列的界面元素,如表单输入、按钮列表等。z
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
约束布局
约束布局是一个功能强大的布局,允许你通过约束来定位和调整子视图的位置和大小。
复杂的界面布局,特别是需要精确控制视图位置和大小的场景,如响应式布局。
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello, World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
表格布局
表格布局允许以行和列的方式组织视图。表格数据展示,如产品列表、日历等。
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row 1, Column 1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row 1, Column 2" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row 2, Column 1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row 2, Column 2" />
</TableRow>
</TableLayout>
帧布局
帧布局将所有子视图堆叠在一起,后添加的视图会覆盖在前一个视图之上。
用于显示单个子视图或需要在同一个区域显示多个视图,如重叠图像、视频播放器等。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Overlay Text" />
</FrameLayout>
相对布局
相对布局允许子视图相对于父视图或其他子视图进行定位。
需要相对定位的布局,如按钮在图像下方,文本在按钮右侧等。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Below Image"
android:layout_below="@id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right of Text"
android:layout_toRightOf="@id/textView" /
>
</RelativeLayout>
控件
按钮点击事件
对于控件 实际上都可以设置点击事件 不仅仅只限于按钮
方法一:xml中 “onClick”属性
xml
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="handleButtonClick" />
java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void handleButtonClick(View view) {
// 处理按钮点击事件
Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show();
}
}
方法二:设置监听器“ setOnClickListener
”
xml
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 处理按钮点击事件
Toast.makeText(MainActivity.this, "Button clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}