随笔 - 12,  文章 - 0,  评论 - 0,  阅读 - 2634

LinearLayout

  • 线性布局是Android里一种非常常用的布局,它会将布局内所有的组件在线性的方向上进行排列!
属性 效果
android:orientation="vertical" 垂直方向线性排列
android:orientation="horizontal" 水平方向线性排列
  • 布局内组件可以使用权重layout_weight

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

  • Button组件是程序和用户交互的按钮组件,可配置属性可参考TextView!
<Button
android:id="@+id/buttonPanel"
android:layout_width="200dp"
android:layout_height="50dp"
android:text="Button"
android:textAllCaps="true">
</Button>
属性 效果
android:textAllCaps 指定组件内英文字母是否自动进行大小写转换
  • button组件点击事件
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 指定输入框的最大行数
  • EditText点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//此处添加逻辑
//初始化输入框组件
EditText editText=findViewById(R.id.edit_query);
//获取输入框的内容
String text=editText.getText().toString();
//Toast弹出提示框 提示框内显示输入框输入的内容
Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
String text1="提示性文本";
//为输入框赋值 登录注册时实现信息回填的功能
editText.setText(text1);
}
});

ImageView

  • 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和HorizontalScrollView

  • 滚动条和横向滚动条,需要注意的是他的内部只能放一个单一组件,一般可以放一个布局组件,再在布局组件内来实现复杂的层级结构!
  • 代码内设置
//隐藏滑块
scrollview.setVerticalScrollBarEnabled(false);
//滚动到底部
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
//滚动到顶部
scrollView.fullScroll(ScrollView.FOCUS_UP);
posted on   剑九名为六千里  阅读(164)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示