programing

XML로 직사각형을 그릴 수 있습니까?

lastcode 2023. 10. 19. 22:20
반응형

XML로 직사각형을 그릴 수 있습니까?

XML로 직사각형을 그릴 수 있는지 궁금합니다. drawRect 방법을 프로그래밍적으로 사용해서 그릴 줄 압니다.

네 가능합니다. 여기 제가 아까 만든 것이 있습니다.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <stroke android:width="2dp" android:color="#ff207d94" />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="5dp" />
    <solid android:color="#ffffffff" />
</shape>

그리기 가능한 폴더 안에 새 XML 파일을 만들고 위의 코드를 추가한 다음 rogen.xml로 저장할 수 있습니다.

레이아웃 내에서 사용하려면android:background새 그리기 가능 형상에 속성을 부여합니다.정의한 모양에는 차원이 없으므로 레이아웃에 정의된 보기의 차원이 적용됩니다.

이 모든 것을 종합해보면,

<View
    android:id="@+id/myRectangleView"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:background="@drawable/rectangle"/>

마지막으로, 이 직사각형을 모든 보기의 배경으로 설정할 수 있습니다(ImageViews의 경우 사용).android:src. 즉, 직사각형을 ListViews, TextViews 등의 배경으로 사용할 수 있습니다.

만들다rectangle.xml도형 그리기 가능 사용 이렇게 그리기 가능 폴더에 넣음...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <solid android:color="@android:color/transparent"/>
   <corners android:radius="12px"/> 
   <stroke  android:width="2dip" android:color="#000000"/>  
</shape>

그것을 에 넣다.ImageView

<ImageView 
android:id="@+id/rectimage" 
android:layout_height="150dp" 
android:layout_width="150dp" 
android:src="@drawable/rectangle">
</ImageView>

이것이 당신에게 도움이 되기를 바랍니다.

빠르고 더러운 방법:

<View
    android:id="@+id/colored_bar"
    android:layout_width="48dp"
    android:layout_height="3dp"
    android:background="@color/bar_red" />

이거 먹어봐요.

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_marginTop="5dp"
                    android:layout_height="wrap_content">

                    <View
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:background="#3fe1fa" />

                    <TextView
                        android:textSize="12dp"
                        android:paddingLeft="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:text="1700 Market Street"
                        android:id="@+id/textView8" />
                </TableRow>

산출량

enter image description here

이 코드 사용

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:radius="0.1dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

    <solid android:color="#Efffff" />

    <stroke
        android:width="2dp"
        android:color="#25aaff" />

</shape>

그릴 수 있는 리소스 파일 생성

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#3b5998" />
<cornersandroid:radius="15dp"/>

먼저 직사각형 벡터 자산을 만든 다음 //이 코드를 직사각형 그리기 가능한 파일 안에 넣고 이 그리기 가능한 파일(rectangle)을 배경으로 사용합니다. 즉, e - 안드로이드:background="@drawable/baseline_rectangle_24"

<?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
        <stroke android:width="2dp" android:color="#C4C4C4" />
        <padding android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp" />
        <corners android:radius="15dp" />
        <solid android:color="#FFFFFF" />
    </shape>

언급URL : https://stackoverflow.com/questions/10124919/can-i-draw-rectangle-in-xml

반응형