Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में पोर्ट्रेट और लैंडस्केप के लिए अलग-अलग लेआउट कैसे बनाएं?

Android में ओरिएंटेशन-विशिष्ट लेआउट (Portrait/Landscape)

डिवाइस ओरिएंटेशन के आधार पर अलग-अलग लेआउट फ़ाइलें स्वचालित रूप से लोड करने का तरीका।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project।

चरण 2: डिफ़ॉल्ट (पोर्ट्रेट) लेआउट

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f6f4"
    android:padding="10dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Show Orientation mode" />

    <TextView
        android:id="@+id/tvLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button"
        android:layout_centerInParent="true"
        android:layout_marginTop="16sp"
        android:text="Portrait Mode"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

चरण 3: लैंडस्केप लेआउट बनाएं

  1. Project व्यू में res पर राइट-क्लिक → New → Android Resource Directory
  2. Resource type: layout, Name: layout
  3. Available qualifiers से Orientation चुनें → >>
  4. Screen orientation: Landscape चुनें → OK
  5. बनी फ़ाइल res/layout-land/activity_main.xml में निम्न कोड डालें:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="128dp"
        android:text="Show Orientation mode" />

    <TextView
        android:id="@+id/tvLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginStart="64dp"
        android:layout_toEndOf="@id/button"
        android:text="Landscape Mode"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

चरण 4: MainActivity.java (Kotlin में)

import android.content.res.Configuration
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    lateinit var textView: TextView
    lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        textView = findViewById(R.id.tvLayout)
        button = findViewById(R.id.button)

        button.setOnClickListener {
            val orientation = resources.configuration.orientation
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                Toast.makeText(this@MainActivity, "We are in portrait mode", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this@MainActivity, "We are in Landscape mode", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

चरण 5: AndroidManifest.xml

मानक मैनिफेस्ट।

ऐप चलाएं

डिवाइस घुमाएं - लेआउट स्वचालित रूप से बदल जाएगा। बटन दबाकर वर्तमान ओरिएंटेशन देखें।

Android में पोर्ट्रेट और लैंडस्केप के लिए अलग-अलग लेआउट कैसे बनाएं? Android में पोर्ट्रेट और लैंडस्केप के लिए अलग-अलग लेआउट कैसे बनाएं?
  1. Android में एक्टिविटी के बीच स्विच कैसे करें? Android में एक्टिविटी के बीच स्विच कैसे करें?

    एक्टिविटी के बीच नेविगेशन यह उदाहरण दर्शाता है कि Intent का उपयोग करके दो एक्टिविटी के बीच आगे-पीछे कैसे नेविगेट करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (activity_main.xml) <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:an

  1. Android में कस्टम नोटिफिकेशन लेआउट और टेक्स्ट कलर कैसे बनाएं? Android में कस्टम नोटिफिकेशन लेआउट और टेक्स्ट कलर कैसे बनाएं?

    Android में कस्टम नोटिफिकेशन लेआउट बनाने का पूरा गाइड यह उदाहरण RemoteViews और NotificationCompat.Builder का उपयोग करके पूरी तरह से कस्टमाइज्ड नोटिफिकेशन (कस्टम लेआउट, इमेज, टेक्स्ट कलर, बटन) बनाने का तरीका दिखाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: म

  1. PC और Android के लिए Google SafeSearch कैसे चालू करें PC और Android के लिए Google SafeSearch कैसे चालू करें

    इंटरनेट दुनिया का सूचना सुपरहाईवे है। इसका मतलब है कि आपके दैनिक उपभोग के लिए ऑनलाइन उपलब्ध टन उपयोगी सामग्री के साथ, बराबर मात्रा में परेशान करने वाली, खतरनाक सामग्री भी है जिससे आप दूर रहना चाहते हैं जब आप अपनी जरूरतों के लिए ऑनलाइन खोज करते हैं। वह अवांछनीय सामग्री एक आकस्मिक खोज शब्द दूर है आपके