[android] 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 파일을 만들고 위 코드를 추가 한 다음 rectangle.xml로 저장할 수 있습니다.

레이아웃 내에서 사용하려면 android:background속성을 새로운 드로어 블 모양으로 설정합니다 . 정의한 모양에는 크기가 없으므로 레이아웃에 정의 된보기의 크기를 사용합니다.

그래서 모두 합치면 :

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

드디어; 이 사각형을 모든 뷰의 배경으로 설정할 수 있지만 ImageView의 경우 android:src. 즉, 사각형을 ListViews, TextViews 등의 배경으로 사용할 수 있습니다.


답변

rectangle.xmlDrawable 모양을 사용하여 만들기 Drawable 폴더에 넣으십시오 .

<?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>

산출

여기에 이미지 설명 입력


답변

이 코드 사용

<?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"/>


답변