سؤال

لدي بعض المشكلات في العثور على وثائق تعريفات الأشكال في XML لنظام Android. أرغب في تحديد دائرة بسيطة مملوءة بلون صلب في ملف XML لتضمينه في ملفات التخطيط الخاصة بي.

للأسف لا تغطي الوثائق على Android.com سمات XML لفئات الشكل. أعتقد أنني يجب أن أستخدم arcshape لرسم دائرة ولكن لا يوجد تفسير حول كيفية ضبط الحجم أو اللون أو الزاوية اللازمة لجعل دائرة من القوس.

هل كانت مفيدة؟

المحلول

هذه دائرة بسيطة كرسومات قابلة للرسمية في Android.

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

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

   <size 
       android:width="120dp"
        android:height="120dp"/>
</shape>

نصائح أخرى

اضبط هذا كخلفية عرضك

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="#78d9ff"/>
</shape>

enter image description here

لاستخدام الدائرة الصلبة:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#48b3ff"/>
</shape>

enter image description here

صلبة مع السكتة الدماغية:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#199fff"/>
    <stroke
        android:width="2dp"
        android:color="#444444"/>
</shape>

enter image description here

ملحوظة: لتصنع oval يظهر الشكل كدائرة ، في هذه الأمثلة ، إما رأيك بأنك تستخدم هذا الشكل لأن خلفيته يجب أن تكون مربعًا أو يجب عليك تعيين ملف height و width خصائص علامة الشكل لقيمة متساوية.

رمز لدائرة بسيطة

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
        <solid android:color="#9F2200"/>
        <stroke android:width="2dp" android:color="#fff" />
        <size android:width="80dp" android:height="80dp"/>
</shape>

انظر في عينات Android SDK. هناك العديد من الأمثلة في مشروع Apidemos:

/apidemos/res/drawable/

  • Black_box.xml
  • شكل _5.xml
  • إلخ

سيبدو مثل هذا لدائرة مع ملء التدرج:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
            android:angle="270"/>
</shape>

يمكنك استخدام VectordRawable على النحو التالي:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="200dp"
    android:viewportHeight="64"
    android:viewportWidth="64">

    <path
        android:fillColor="#ff00ff"
        android:pathData="M22,32
        A10,10 0 1,1 42,32
        A10,10 0 1,1 22,32 Z" />
</vector>

يقدم XML أعلاه:

enter image description here

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

    <!-- fill color -->
    <solid android:color="@color/white" />

    <!-- radius -->
    <stroke
        android:width="1dp"
        android:color="@color/white" />

    <!-- corners -->
    <corners
        android:radius="2dp"/>
</shape>

إليك سيارة Circle_background.xml بسيطة لما قبل المادية:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@color/color_accent_dark" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/color_accent" />
        </shape>
    </item>
</selector>

يمكنك استخدام السمة 'android:background="@drawable/circle_background" في تعريف تخطيط الزر الخاص بك

إذا كنت تريد أ دائرة مثله

enter image description here

حاول استخدام الرمز أدناه:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="2"
    android:useLevel="false" >
    <solid android:color="@android:color/white" />
    <stroke
        android:width="1dp"
        android:color="@android:color/darker_gray" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <stroke
        android:width="10dp"
        android:color="@color/white"/>

    <gradient
        android:startColor="@color/red"
        android:centerColor="@color/red"
        android:endColor="@color/red"
        android:angle="270"/>

    <size 
        android:width="250dp" 
        android:height="250dp"/>
</shape>

يمكنك تجربة هذا -

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="700"
    android:thickness="100dp"
    android:useLevel="false">

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

</shape>

أيضًا ، يمكنك ضبط نصف قطر الدائرة عن طريق ضبط android:thickness.

enter image description here

لم أتمكن من رسم دائرة داخل CorrintLayout لسبب ما ، لم أستطع استخدام أي من الإجابات أعلاه.

ما الذي نجح فيه هو عرض نص بسيط مع النص الذي يخرج ، عندما تضغط على "Alt+7":

 <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0075bc"
            android:textSize="40dp"
            android:text="•"></TextView>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/text_color_green"/>
            <!-- Set the same value for both width and height to get a circular shape -->
            <size android:width="250dp" android:height="250dp"/>
        </shape>
    </item>
</selector>

الدقة/draw/circle_shape.xml

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

        <shape android:shape="oval">
            <solid android:color="#e42828"/>
            <stroke android:color="#3b91d7" android:width="5dp"/>
            <!-- Set the same value for both width and height to get a circular shape -->
            <size android:width="250dp" android:height="250dp"/>
        </shape>
    </item>
</selector>

enter image description here

مثال على الإنترنت :https://code-droid-example.blogspot.com/2019/07/how-to-circle-shape-in-xml-drawable-android.html

فقط استخدم

ShapeDrawable circle = new ShapeDrawable( new  OvalShape() );
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top