RadioButton
- 常用属性
- 自定义样式
- 监听事件
选择效果图
RadioButton代码
activity_radio_button.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<RadioGroup
android:id="@+id/rg_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textColor="#FF9800"
android:textSize="18sp"
android:checked="true"/>
<RadioButton
android:id="@+id/rb_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rb_1"
android:text="女"
android:textColor="#FF9800"
android:textSize="18sp"/>
</RadioGroup>
<RadioGroup
android:id="@+id/rg_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rg_1"
android:orientation="horizontal"
android:layout_marginTop="50dp">
<RadioButton
android:id="@+id/rb_3"
android:layout_width="60dp"
android:layout_height="30dp"
android:gravity="center"
android:text="男"
android:textColor="#000"
android:textSize="18sp"
android:checked="true"
android:button="@null"
android:background="@drawable/selector_orange_radiobtn"
/>
<RadioButton
android:id="@+id/rb_4"
android:layout_width="60dp"
android:layout_height="30dp"
android:gravity="center"
android:text="女"
android:textColor="#000"
android:textSize="18sp"
android:button="@null"
android:layout_marginLeft="10dp"
android:background="@drawable/selector_orange_radiobtn"
/>
</RadioGroup>
</RelativeLayout>
选项背景效果
selector_orange_radiobtn.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="#9C640B" />
<corners android:radius="5dp" />
</shape>
</item>
<item android:state_checked="false">
<shape>
<stroke
android:width="1dp"
android:color="#97723A" />
<corners android:radius="5dp" />
</shape>
</item>
</selector>
RadioButtonActivity代码
public class RadioButtonActivity extends AppCompatActivity {
private RadioGroup mRG1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);
mRG1 = findViewById(R.id.rg_1);
mRG1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = group.findViewById(checkedId);
Toast.makeText(RadioButtonActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
CheckBox
- 常用属性
- 自定义样式
- 监听事件
复选框效果图
复选框效果图
代码
activity_check_box.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你会哪些移动开发:"
android:textColor="#000"
android:layout_marginTop="10dp"/>
<CheckBox
android:id="@+id/cb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:layout_below="@id/tv_title"/>
<CheckBox
android:id="@+id/cb_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="iOS"
android:layout_below="@id/cb_1"/>
<CheckBox
android:id="@+id/cb_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H5"
android:layout_below="@id/cb_2"/>
<CheckBox
android:id="@+id/cb_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其它"
android:layout_below="@id/cb_3"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@id/cb_4"
android:layout_marginTop="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的兴趣:"
android:textSize="20sp"
android:textColor="#000"
/>
<CheckBox
android:id="@+id/cb_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编程"
android:layout_below="@id/cb_5"
android:layout_marginTop="10dp"
android:button="@drawable/bg_checkbox"
android:paddingLeft="10dp"
/>
<CheckBox
android:id="@+id/cb_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="做饭"
android:layout_below="@id/cb_5"
android:button="@drawable/bg_checkbox"
android:paddingLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>
CheckBoxActivity
public class CheckBoxActivity extends AppCompatActivity {
private CheckBox mCb5, mCb6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
mCb5 = findViewById(R.id.cb_5);
mCb6 = findViewById(R.id.cb_6);
mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this, isChecked ? "5选中" : "5未选中", Toast.LENGTH_SHORT).show();
}
});
mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this, isChecked ? "6选中" : "6未选中", Toast.LENGTH_SHORT).show();
}
});
}
}
ImageView
- 常用属性
- 加载网络图片
scaleType
- fitXY: 撑满控件,宽高比可能发生改变
- fitCenter: 保持宽高比缩放,直至完全显示
- centerCrop: 保持宽高比缩放,直至完全覆盖控件,裁剪显示
效果图
ImageView效果图代码
activity_image_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<ImageView
android:id="@+id/iv_1"
android:layout_width="300dp"
android:layout_height="150dp"
android:background="#FF9800"
android:src="@drawable/bg_ironman"
android:scaleType="fitXY"
/>
<ImageView
android:id="@+id/iv_2"
android:layout_width="300dp"
android:layout_height="150dp"
android:background="#FF9800"
android:src="@drawable/bg_ironman"
android:scaleType="fitCenter"
android:layout_below="@id/iv_1"/>
<ImageView
android:id="@+id/iv_3"
android:layout_width="300dp"
android:layout_height="150dp"
android:background="#FF9800"
android:src="@drawable/bg_ironman"
android:scaleType="centerCrop"
android:layout_below="@id/iv_2"
/>
<ImageView
android:id="@+id/iv_4"
android:layout_width="300dp"
android:layout_height="150dp"
android:background="#FF9800"
android:scaleType="centerCrop"
android:layout_below="@id/iv_3"
/>
</RelativeLayout>
ImageViewActivity文件
public class ImageViewActivity extends AppCompatActivity {
private ImageView mIv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
mIv4 = findViewById(R.id.iv_4);
Glide.with(this).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603459341312&di=bc520d0ed58b8e3786e74ef3f8442985&imgtype=0&src=http%3A%2F%2Fz1.dfcfw.com%2F2020%2F10%2F14%2F20201014075848343834694.jpg").into(mIv4);
}
}
添加第三方库
repositories {
google()
jcenter()
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
使用Sync Now后自动导入图片加载库
网友评论