RadioButton
- 常用屬性
- 自定義樣式
- 監(jiān)聽(tīng)事件
選擇效果圖
代碼
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>
選項(xiàng)背景效果
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
- 常用屬性
- 自定義樣式
- 監(jiān)聽(tīng)事件
復(fù)選框效果圖
代碼
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="你會(huì)哪些移動(dòng)開(kāi)發(fā):"
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
- 常用屬性
- 加載網(wǎng)絡(luò)圖片
scaleType
- fitXY: 撐滿控件绵脯,寬高比可能發(fā)生改變
- fitCenter: 保持寬高比縮放,直至完全顯示
- centerCrop: 保持寬高比縮放休里,直至完全覆蓋控件蛆挫,裁剪顯示
效果圖
代碼
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);
}
}
添加第三方庫(kù)
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后自動(dòng)導(dǎo)入圖片加載庫(kù)