XML का उपयोग करके TextView पर बाएं बॉर्डर जोड़ना
यह ट्यूटोरियल दिखाता है कि ड्रॉएबल रिसोर्स का उपयोग करके TextView पर केवल बाईं ओर बॉर्डर कैसे बनाया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें और आवश्यक विवरण भरें।
चरण 2: लेआउट फ़ाइल बनाएं (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_border_bg"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="नमस्ते दुनिया!"
android:textSize="30sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
चरण 3: ड्रॉएबल रिसोर्स बनाएं (res/drawable/left_border_bg.xml)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<!-- पृष्ठभूमि रंग (बॉर्डर रंग) -->
<item>
<shape android:shape="rectangle">
<solid android:color="#FF5722" /> <!-- नारंगी बॉर्डर -->
</shape>
</item>
<!-- आंतरिक सामग्री क्षेत्र (बाएं 4dp छोड़कर) -->
<item android:left="4dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" /> <!-- सफेद पृष्ठभूमि -->
</shape>
</item>
</layer-list>
चरण 4: MainActivity.java
package com.example.leftborderdemo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
कैसे काम करता है
layer-list दो आयतों को स्टैक करता है:
- पहला आइटम: पूर्ण आकार का आयत बॉर्डर रंग के साथ (नारंगी #FF5722)
- दूसरा आइटम:
android:left="4dp"वाला आयत, जो बाईं ओर 4dp का अंतर छोड़ता है, जिससे बॉर्डर दिखाई देता है
ऐप चलाएं
डिवाइस पर ऐप चलाएं। TextView के बाईं ओर 4dp मोटी नारंगी बॉर्डर दिखाई देगी।
कस्टमाइज़ेशन: बॉर्डर रंग बदलने के लिए पहले <solid> का रंग बदलें। मोटाई बदलने के लिए android:left मान समायोजित करें।