To create a button with an outer white boundary, inner grey background, an image, and text in Android, you can use a combination of a LayerDrawable
and a TextView
inside a FrameLayout
. Here’s how you can achieve this:
- Create a new drawable resource file for the button background (
button_background.xml
):
xml"1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/grey"/>
<stroke android:color="@color/white" android:width="4dp"/>
shape>
item>
layer-list>
- Create another drawable resource file for the button icon (
button_icon.xml
), assuming the right tick image is namedic_right_tick
:
xml"1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_right_tick"
android:gravity="center"/>
- In your layout file, add the following code to create the button:
xml<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/button_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/button_icon"/>
<TextView
android:id="@+id/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Apply"
android:textColor="@color/white"
android:textSize="16sp"/>
FrameLayout>
- Finally, in your Java code, set the background of the
FrameLayout
to the button background drawable:
javaFrameLayout button = findViewById(R.id.button);
button.setBackgroundResource(R.drawable.button_background);
This will create a button with an outer white boundary, inner grey background, an image (right tick), and text (Apply). Adjust the sizes and positions as needed to match your design.
المزيد من المعلومات
لإنشاء زر في تطبيق Android يحتوي على حدود بيضاء اللون في الخارج وخلفية رمادية اللون في الداخل، بالإضافة إلى صورة (علامة صح) ونص (تطبيق)، يمكنك استخدام عنصر LayerDrawable
مع عنصر TextView
داخل FrameLayout
. إليك كيفية تحقيق ذلك:
- قم بإنشاء ملف drawable جديد لخلفية الزر (
button_background.xml
):
xml"1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/grey"/>
<stroke android:color="@color/white" android:width="4dp"/>
shape>
item>
layer-list>
- قم بإنشاء ملف drawable آخر لأيقونة الزر (
button_icon.xml
)، افترضا أن الصورة المستخدمة لعلامة الصح تسمىic_right_tick
:
xml"1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_right_tick"
android:gravity="center"/>
- في ملف الـ layout الخاص بك، قم بإضافة الكود التالي لإنشاء الزر:
xml<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/button_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/button_icon"/>
<TextView
android:id="@+id/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="تطبيق"
android:textColor="@color/white"
android:textSize="16sp"/>
FrameLayout>
- أخيرًا، في الشيفرة الخاصة بك في Java، ضع خلفية
FrameLayout
إلى drawable الخاص بخلفية الزر:
javaFrameLayout button = findViewById(R.id.button);
button.setBackgroundResource(R.drawable.button_background);
هذا سينشئ زرًا بحدود بيضاء في الخارج، خلفية رمادية في الداخل، صورة (علامة صح) ونص (تطبيق). قم بتعديل الأحجام والمواضع حسب التصميم الخاص بك.