البرمجة

زر تطبيق بحدود بيضاء وخلفية رمادية

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:

  1. 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>
  1. Create another drawable resource file for the button icon (button_icon.xml), assuming the right tick image is named 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"/>
  1. 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>
  1. Finally, in your Java code, set the background of the FrameLayout to the button background drawable:
java
FrameLayout 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. إليك كيفية تحقيق ذلك:

  1. قم بإنشاء ملف 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>
  1. قم بإنشاء ملف 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"/>
  1. في ملف الـ 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>
  1. أخيرًا، في الشيفرة الخاصة بك في Java، ضع خلفية FrameLayout إلى drawable الخاص بخلفية الزر:
java
FrameLayout button = findViewById(R.id.button); button.setBackgroundResource(R.drawable.button_background);

هذا سينشئ زرًا بحدود بيضاء في الخارج، خلفية رمادية في الداخل، صورة (علامة صح) ونص (تطبيق). قم بتعديل الأحجام والمواضع حسب التصميم الخاص بك.

زر الذهاب إلى الأعلى