LinearLayout
- 线性布局是Android里一种非常常用的布局,它会将布局内所有的组件在线性的方向上进行排列!
属性 |
效果 |
android:orientation="vertical" |
垂直方向线性排列 |
android:orientation="horizontal" |
水平方向线性排列 |
RelativeLayout
- 相对布局也是Android里一种非常常用的布局,他可以通过相对定位的方式来让组件出现在布局内的任何位置,同时相对布局的属性也会比线性布局多很多!
- 相对布局和线性布局配合使用可以实现大部分布局需求。
- 相对于父布局
属性 |
效果 |
android:layout_centerHrizontal |
水平居中 |
android:layout_centerVertical |
垂直居中 |
android:layout_centerInparent |
相对于父布局完全居中 |
android:layout_alignParentBottom |
位于父元素的下边缘 |
android:layout_alignParentLeft |
位于父元素的左边缘 |
android:layout_alignParentRight |
位于父元素的右边缘 |
android:layout_alignParentTop |
位于父元素的上边缘 |
属性 |
效果 |
android:layout_below |
位于元素的下方 |
android:layout_above |
位于元素的的上方 |
android:layout_toLeftOf |
位于元素的左边 |
android:layout_toRightOf |
位于元素的右边 |
android:layout_alignTop |
该元素的上边缘和某元素的的上边缘对齐 |
android:layout_alignLeft |
该元素的左边缘和某元素的的左边缘对齐 |
android:layout_alignBottom |
该元素的下边缘和某元素的的下边缘对齐 |
android:layout_alignRight |
该元素的右边缘和某元素的的右边缘对齐 |
属性 |
效果 |
android:layout_marginBottom |
底边缘的距离 |
android:layout_marginLeft |
左边缘的距离 |
android:layout_marginRight |
右边缘的距离 |
android:layout_marginTop |
上边缘的距离 |
Textview
| <TextView |
| android:id="@+id/textv" |
| android:layout_width="match_parent" |
| android:layout_height="wrap_content" |
| android:text="文字组件" |
| android:textSize="50dp" |
| android:textColor="#00ffff" |
| android:background="@drawable/ic_launcher_background" |
| android:layout_gravity="center" |
| android:gravity="center"> |
| </TextView> |
属性 |
效果 |
android:id |
指定组件的唯一标识符 |
android:layout_width="match_parent |
设置组件的宽等于父布局的宽 |
android:layout_width="wrap_content" |
设置组件的宽由组件本身大小决定 |
android:layout_height |
设置组件的高 两个属性值与宽同理 |
android:text |
指定组件需要显示的文本内容 |
android:textSize |
设置文本内容文字的大小 |
android:textColor |
设置文本内容文字的颜色 |
android:background |
设置文本内容的背景样式 |
android:layout_gravity |
设置组件相对于父布局的对齐方式 |
android:gravity |
设置组件内内容的对齐方式 |
- Button组件是程序和用户交互的按钮组件,可配置属性可参考TextView!
| <Button |
| android:id="@+id/buttonPanel" |
| android:layout_width="200dp" |
| android:layout_height="50dp" |
| android:text="Button" |
| android:textAllCaps="true"> |
| </Button> |
属性 |
效果 |
android:textAllCaps |
指定组件内英文字母是否自动进行大小写转换 |
| public class MainActivity extends AppCompatActivity { |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| setContentView(R.layout.activity_main); |
| |
| |
| Button button=findViewById(R.id.buttonPanel); |
| button.setOnClickListener(new View.OnClickListener() { |
| @Override |
| public void onClick(View v) { |
| |
| } |
| }); |
| } |
| } |
EditText
| <EditText |
| android:id="@+id/edit_query" |
| android:layout_width="match_parent" |
| android:layout_height="wrap_content" |
| android:background="#FFEB3B" |
| android:hint="提示性文本" |
| android:layout_margin="10dp" |
| android:padding="10dp" |
| android:minLines="2"> |
| </EditText> |
属性 |
效果 |
android:hint |
为输入框指定一段提示文本 |
layout_margin |
组件的外边距 |
android:padding |
组件的内边距 |
android:minLines |
指定输入框的最大行数 |
| button.setOnClickListener(new View.OnClickListener() { |
| @Override |
| public void onClick(View v) { |
| |
| |
| EditText editText=findViewById(R.id.edit_query); |
| |
| String text=editText.getText().toString(); |
| |
| Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show(); |
| String text1="提示性文本"; |
| |
| editText.setText(text1); |
| } |
| }); |
ImageView
| <ImageView |
| android:id="@+id/image" |
| android:layout_width="200dp" |
| android:layout_height="200dp" |
| android:src="@mipmap/ic_launcher"> |
| </ImageView> |
属性 |
效果 |
android:src |
给组件指定一张图片(图片通常放在drawable目录下) |
| ImageView imageView=findViewById(R.id.image); |
| imageView.setImageResource(R.drawable.ic_launcher_foreground); |
| |
RadioGroup
- 单选按钮RadioButton只能选中一个,所有需要把他放在RadioGroup中!
| <RadioGroup |
| android:layout_width="340dp" |
| android:layout_height="50dp" |
| android:layout_margin="5dp" |
| android:gravity="center" |
| android:orientation="horizontal"> |
| <TextView |
| android:paddingLeft="10dp" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:text="请选择性别:" |
| android:textSize="18dp" |
| android:textColor="#ffffff" |
| > |
| </TextView> |
| <RadioButton |
| android:id="@+id/m2r1" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:text="男" |
| android:checked="true" |
| android:textColor="#ffffff"> |
| </RadioButton> |
| <RadioButton |
| android:id="@+id/m2r2" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:text="女" |
| android:textColor="#ffffff"> |
| </RadioButton> |
| </RadioGroup> |
属性 |
效果 |
android:checked |
设置单选按钮是否默认选中 |
| if (radioButton1.isChecked()){ |
| gender="男"; |
| }else{ |
| gender="女"; |
| } |
CheckBox
- 用户可以通过复选框组件点击的方式来进行选中或者取消,可以用这个控件来实现记住密码等功能!
| <CheckBox |
| android:id="@+id/checkbox" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content"> |
| </CheckBox> |
| CheckBox checkBox=findViewById(R.id.checkbox); |
| checkBox.isChecked(); |
| |
- 滚动条和横向滚动条,需要注意的是他的内部只能放一个单一组件,一般可以放一个布局组件,再在布局组件内来实现复杂的层级结构!
- 代码内设置
| |
| scrollview.setVerticalScrollBarEnabled(false); |
| |
| scrollView.fullScroll(ScrollView.FOCUS_DOWN); |
| |
| scrollView.fullScroll(ScrollView.FOCUS_UP); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?