且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将形状添加到LinearLayout Android

更新时间:2023-02-13 12:49:05

您应该将ShapeDrawable用于要将样式添加到的组件的背景.

You should use a ShapeDrawable for the background of the component you want to add the style to.

使用以下语法在XML中创建可绘制形状(此形状显示带有圆角的矩形,但是可以根据需要自定义外观很多).此文件(名为background_square.xml或其他名称)应放在您的可绘制文件夹中:

A shape drawable is created in XML, with the following syntax (this one shows a rectangle, with the rounded corners, but there is lots you can do to customize this to look however you want). This file (named background_square.xml or whatever) should be put in your drawable folder:

 <?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp" />
    <solid
        android:color="@color/primary_grey" /> 
</shape>

然后,当您要将其添加到视图时,可以在XML中使用以下语法:

Then, when you want to add it to a View, you can use the following syntax in your XML:

android:background="@drawable/background_square"