一手遮天 Android - UI: shape 之填充,描边,圆角,尺寸,内部间距

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - UI: shape 之填充,描边,圆角,尺寸,内部间距

示例如下:

/ui/ShapeDemo2.java

/**
 * shape 之填充,描边,圆角,尺寸,内部间距
 */

package com.webabcd.androiddemo.ui;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import com.webabcd.androiddemo.R;

public class ShapeDemo2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ui_shapedemo2);
    }
}

/layout/activity_ui_shapedemo2.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">

    <!--shape 之填充、描边、圆角,参见 res/drawable/shape_sample_solid_stroke_corners.xml-->
    <TextView
        android:id="@+id/textView1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/shape_sample_solid_stroke_corners" />

    <!--shape 之尺寸,参见 res/drawable/shape_sample_size.xml-->
    <TextView
        android:id="@+id/textView2"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:background="@color/orange"
        android:drawableBottom="@drawable/shape_sample_size"/>

    <!--shape 之内部间距,参见 res/drawable/shape_sample_padding.xml-->
    <TextView
        android:id="@+id/textView3"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:text="你好 webabcd"
        android:background="@drawable/shape_sample_padding"/>

</LinearLayout>

/res/drawable/shape_sample_solid_stroke_corners.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        solid - 填充
            color - 填充的颜色

        stroke - 描边
            color - 描边的颜色
            width - 描边的宽度
            dashWidth - 每段虚线的实线部分的长度(指定为 0 的话则没有虚线)
            dashGap - 每段虚线的空白部分的长度(指定为 0 的话则没有虚线)

        corners - 圆角
            radius - 圆角半径
            topLeftRadius - 左上角的圆角的半径
            topRightRadius - 右上角的圆角的半径
            bottomLeftRadius - 左下角的圆角的半径
            bottomRightRadius - 右下角的圆角的半径
    -->

    <solid android:color="#f00" />
    <stroke
        android:color="#00f"
        android:width="20dp"
        android:dashWidth="50dp"
        android:dashGap="10dp" />
    <corners android:radius="20dp" />
</shape>

/res/drawable/shape_sample_size.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!--
        size - 尺寸
            width - 宽度
            height - 高度
    -->

    <!--
        size 不一定会生效的
        1、比如通过 background 引用此 xml 的话则 size 不生效,此矩形的宽高会与引用此 xml 的 view 的宽高一致
        2、如果通过 drawableBottom 引用此 xml 的话则 size 就会生效
    -->

    <solid android:color="#00f" />
    <size android:width="65535dp" android:height="10dp" />
</shape>

/res/drawable/shape_sample_padding.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        padding - 内部间距
            left - 左间距
            top - 上间距
            right - 右间距
            bottom - 下间距
    -->

    <solid android:color="#0f0" />
    <padding android:top="10dp" android:left="10dp" />
</shape>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-06-02 08:48  webabcd  阅读(240)  评论(0编辑  收藏  举报