एंड्रॉइड व्यू पर केवल ऊपर और नीचे बॉर्डर जोड़ना
यह उदाहरण दिखाता है कि <layer-list> ड्रॉएबल का उपयोग करके किसी भी व्यू (TextView, Button, आदि) पर केवल ऊपर और नीचे बॉर्डर कैसे बनाया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।
चरण 2: बॉर्डर ड्रॉएबल बनाएं
res/drawable/border_top_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<!-- नीचे की लेयर: पूरा आयताकार बॉर्डर -->
<item>
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#e10606" />
<solid android:color="#9bce64" />
</shape>
</item>
<!-- ऊपर की लेयर: केवल बाएं-दाएं बॉर्डर के साथ आयत, ऊपर-नीचे इनसेट -->
<item
android:top="2dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#9bce64" />
<solid android:color="#9bce64" />
</shape>
</item>
</layer-list>
व्याख्या:
- पहला `
- ` पूरा लाल बॉर्डर + हरा बैकग्राउंड बनाता है
- दूसरा `
- ` `top="2dp"` और `bottom="2dp"` से ऊपर-नीचे 2dp इनसेट करता है
- दूसरे आइटम का हरा स्ट्रोक पहले के लाल स्ट्रोक को ऊपर-नीचे से ढक देता है
- परिणाम: केवल ऊपर और नीचे लाल बॉर्डर दिखता है
चरण 3: लेआउट में उपयोग करें
res/layout/top_bottom_border_in_android_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="एंड्रॉइड व्यू पर ऊपर-नीचे बॉर्डर जोड़ना"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginBottom="16dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/border_top_bottom"
android:padding="30dp"
android:text="TextView पर ऊपर-नीचे बॉर्डर"
android:textColor="#000000"
android:textSize="18sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/border_top_bottom"
android:text="Button पर ऊपर-नीचे बॉर्डर" />
</LinearLayout>
चरण 4: Strings
res/values/strings.xml:
<resources>
<string name="app_name">एंड्रॉइड व्यू पर बॉर्डर जोड़ना</string>
</resources>
चरण 5: MainActivity
MainActivity.java:
package com.example.sample1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// कस्टम लेआउट फाइल उपयोग करें
setContentView(R.layout.top_bottom_border_in_android_view);
}
}
चरण 6: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:dist="https://schemas.android.com/apk/distribution"
package="com.example.sample1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
🎨 विविधताएं
केवल बाएं-दाएं बॉर्डर:
<item android:left="2dp" android:right="2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="@color/green" />
<solid android:color="@color/green" />
</shape>
</item>
डैश्ड (बिंदीदार) बॉर्डर:
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#e10606"
android:dashWidth="10dp"
android:dashGap="5dp" />
<solid android:color="#9bce64" />
</shape>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। TextView और Button पर केवल ऊपर और नीचे लाल बॉर्डर दिखाई देंगे।