android shape 按钮背景_Android button, xml文件定义形状,代码中修改背景颜色
1. 首先在drawable文件夾定義一個shape.xml文件,內容如下:
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp" />
android:left="30dp"
android:top="0dp"
android:right="30dp"
android:bottom="0dp"
/>
2. 在main.xml文件中,button使用這個shape.xml,如下:
android:id="@+id/button_next"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/button_next"
android:textSize="@dimen/label_text_size"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal|center_vertical"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/shape"
/>
這時,該button就顯示如shape.xml定義的形狀。
3. 在代碼中動態修改button 背景顏色,代碼如下:
GradientDrawable bgShape = (GradientDrawable)buttonNext.getBackground();
bgShape.setColor(Color.BLUE);
總結
以上是生活随笔為你收集整理的android shape 按钮背景_Android button, xml文件定义形状,代码中修改背景颜色的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机号脱敏处理_C#简单代码实现对手机号
- 下一篇: pr防抖动效果在哪里(用PR修复视频画面