2022-10-19学习内容
1.形状图形
1.1activity_drawable_shape.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:id="@+id/v_content" android:layout_width="match_parent" android:layout_height="200dp" android:layout_margin="10dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_rect" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="圆角矩形背景"/> <Button android:id="@+id/btn_oval" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="椭圆背景"/> </LinearLayout> </LinearLayout>
1.2shape_rect_gold.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 指定了形状内部的填充颜色 --> <solid android:color="#ffdd66" /> <!-- 指定了形状轮廓的粗细与颜色 --> <stroke android:width="1dp" android:color="#aaaaaa" /> <!-- 指定了形状四个圆角的半径 --> <corners android:radius="10dp" /> </shape>
1.3shape_oval_rose.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!-- 指定了形状内部的填充颜色 --> <solid android:color="#ff66aa" /> <!-- 指定了形状轮廓的粗细与颜色 --> <stroke android:width="1dp" android:color="#aaaaaa" /> </shape>
1.4DrawableShapeActivity.java
package com.example.chapter05; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class DrawableShapeActivity extends AppCompatActivity implements View.OnClickListener { private View v_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drawable_shape); v_content = findViewById(R.id.v_content); findViewById(R.id.btn_rect).setOnClickListener(this); findViewById(R.id.btn_oval).setOnClickListener(this); // v_content的背景设置为圆角矩形 v_content.setBackgroundResource(R.drawable.shape_rect_gold); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_rect: v_content.setBackgroundResource(R.drawable.shape_rect_gold); break; case R.id.btn_oval: v_content.setBackgroundResource(R.drawable.shape_oval_rose); break; } } }
1.5效果:
2.点9图片
2.1activity_drawable_nine.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_normal_orig" android:text="普通图片背景"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_normal" android:layout_marginTop="5dp" android:text="九宫格图片背景"/> </LinearLayout>
2.2效果:
3.状态列表图形
3.1btn_nine_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <item android:drawable="@drawable/button_normal" /> </selector>
3.2activity_drawable_state.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="默认样式的按钮"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_nine_selector" android:text="定制样式的按钮" android:layout_marginTop="5dp" android:padding="5dp"/> </LinearLayout>
3.3效果: