anroid
1.设置内容在框内的位置使用 padding 属性(内边距)
注意:使用start layout_marginStartAPI的版本必须大于17
android :padding = “16dp”
Android:paddingLeft=“16dp”
android:paddingStart=“16dp”==Android:paddingLeft=“16dp”
android:paddingEnd=“16dp”==Android:paddindRight=“16dp”
2.设置框外与组件的距离 使用margin 属性(外边距)
注意:使用start layout_marginStartAPI的版本必须大于17
android:layout_marginStart=“16dp” == Android:layout_marginLeft=“16dp”
android:layout_marginEnd=“16dp” == Android:layout_marginRight=“16dp”
Android:layout_marginTop=“16dp”
Android:layout_marginBottom=“16dp”
sdk大于8就可以使用(2的写法,其作用一毛一样)
android:layout_width=“FILL_PARENT”
android: layout_height_height=“MATCH_PARENT”//继承父级宽度
android:gravity=“centere”//组件的排列方式
android:ignoreGravity="@id/"//设置不跟随gravity的布局方式
android:layout_gravity=“centere”//内容的排列方式
//线性布局管理器
anrdroid:orientation = "vertical" 垂直排列
每一列只能放置一个组件,超出列的宽度不会显示
anrdroid:orientation = "horizontal" 水平排列
每一行只能放置一个组件,超出行的高度将不会显示
//帧布局管理器
层叠的方式去放置组件(表盘的实列)
//前景照片,默认是铺满全屏的显示
android: foreground="@drawable/图片资源"
//将前景照片放置到相应的位置
android:forefroundGravity=“right | bottom”(右下角)
//表格布局管理器
//隐藏列
android:collapseColumns=“1”//隐藏第二列和数组的排列方式相同
android:stretchColumns=“1”//列拉伸
添加行
每添加一个组件就占一列多个组件就占多列
android:columnCount://最大列数
android:rowCount://最大行数
单选按钮
android:checked=“true” //默认选中
<RadioGroup (添加默认的宽高)>
//包裹这点选按钮RadioButton使其成为一组按钮
RadioGroup的监听事件
//获取id
RadioGroup rg = findViewById(R.id.radiogroup1);
//改变选项的监听事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton rg1 = findViewById(i);
//提示消息输出
Toast.makeText(MainActivity.this,"性别为"+rg1.getText(),Toast.LENGTH_SHORT).show();
}
});
}
第二种点击按钮获取点选框的值
Button btn_tj = (Button) findViewById(R.id.btn_tj);
btn_tj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//rg.getChildCount()获取点选按钮的个数
for (int i =0;i<rg.getChildCount();i++){
//获取当前的按钮参数就是当前的循环变量i
RadioButton r = (RadioButton) rg.getChildAt(i);
//通过if语句判断单选按钮是否被点中
if (r.isChecked()){
Toast.makeText(MainActivity.this, r.getText(), Toast.LENGTH_SHORT).show();
break;
}
}
}
});
复选框
android:checked=“true” //默认选中
checkBox = (CheckBox) findViewById(R.id.check_lq);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//当复选框的选择改变时触发事件
//判断复选框是否被选中
if (checkBox.isChecked()){
checkBox.getText();
Toast.makeText(MainActivity.this, checkBox.getText() , Toast.LENGTH_SHORT).show();
}
}
});
进度条
默认属性值是圆形进度条
<ProgressBar
样式
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
最大宽度
android:max="100"
当前进度
android:progress="50"
/>
进度条
<SeekBar
android:id="@+id/seekbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="10"
android:thumb=""//设置进度点为图片
/>
``
seekBar = (SeekBar) findViewById(R.id.seekbar1);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
//进度改变的事件 第二参数是表示进度
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Toast.makeText(MainActivity.this, "进度改变"+i, Toast.LENGTH_SHORT).show();
}
@Override
//开始触摸的事件
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "开始触摸", Toast.LENGTH_SHORT).show();
}
@Override
//停止触摸的事件
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "停止触摸", Toast.LENGTH_SHORT).show();
}
});
星级评分组件
默认5个星
<RatingBar
android:id="@+id/radiobar"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:numStars=“7”
android:rating=“2”
android:stepSize=“1”
android:isIndicator=“true” 是否可以修改
/>
推荐阅读